views:

37

answers:

1

i want to add some options when a user presses the menu button, i have tried the following but nothing seems to happen when i run the app and press the menu button:

the menu xml file

<?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
         <item android:id="@+id/Map_view"
         android:title="@string/Map_view"
         android:icon="@drawable/satellite_view">
     <menu>
     <group android:checkableBehavior="single"
            android:id="@+id/menu_items">              
        <item android:id="@+id/satellite_view"
              android:icon="@drawable/satellite_view"
              android:title="@string/satellite_view"/>

        <item android:id="@+id/street_view"
              android:icon="@drawable/street_view"
              android:title="@string/street_view"/>
      </group>
     </menu>
  </item>              
</menu>

the MapAcitivity class

    package com.example.MyMapActivity;
    import java.util.List;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
     import android.view.MenuItem;

    import com.google.android.maps.GeoPoint;
     import com.google.android.maps.MapActivity;
     import com.google.android.maps.MapController;
     import com.google.android.maps.MapView;
      import com.google.android.maps.Overlay;
     import com.google.android.maps.OverlayItem;

     public class MyMapActivity extends MapActivity {

    private MapView mapview;
    private MapController mapcontroller;
    private GeoPoint gp;


    //creating a class to for OverLays


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //setting the zoom controls
        mapview = (MapView)findViewById(R.id.mapview);
        mapview.setBuiltInZoomControls(true);
        mapview.setSatellite(true);


        List<Overlay>mapOverlays = mapview.getOverlays();
        // pass the drawable to instance of the OVerlay class.
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        MyOverlay overlay = new MyOverlay(drawable,this);


       //setting a GeoPoint
        mapcontroller=mapview.getController();
        String coordinates[] = {"23.68855","86.97455"};
        double lat = Double.parseDouble(coordinates[0]);
        double longi = Double.parseDouble(coordinates[1]);
        gp = new GeoPoint((int)(lat*1E6), (int)(longi*1E6));

        OverlayItem overlayitem = new OverlayItem(gp, "Hello ", "I'm in Asansol");

        GeoPoint point2 = new GeoPoint(35410000, 139460000);
        OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");


        overlay.addOverlay(overlayitem); //  call this method for each overlayitem 
        overlay.addOverlay(overlayitem2);
        mapOverlays.add(overlay);
        }
    // to inflate the menu defined in the map_menu xml file    
      public boolean OnCreateOptionsMenu(Menu menu)
      {
       MenuInflater  inflator = getMenuInflater();
       inflator.inflate(R.menu.map_menu, menu);
       return true;
     }

     public boolean OnOptionsItemSelected(MenuItem item)
     {
       switch(item.getItemId())
       {
       case R.id.satellite_view:
           if(item.isChecked())
           mapview.setSatellite(true);
           return true;
       case R.id.street_view:
           if(item.isChecked())
           mapview.setStreetView(true);
               return true;
        default:
            return super.onOptionsItemSelected(item);
       }
     }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        // this method must return true if your app returns driving directions , else return false
        return false;
    }
}
+3  A: 

Short answer: onCreateOptionsMenu has to start with a lowercase o, not an uppercase O.

Longer answer: In Java, method names always start with a lowercase letter (by convention, not because the Java compiler enforces it), and Android follows the convention faithfully. You intended to override Activity.onCreateOptionsMenu, but actually created a brand new method, MyMapActivity.OnCreateOptionsMenu. You're allowed to create whatever methods you want, of course, so the compiler says nothing. Whenever you are intending to override a method from a superclass, you should mark it with @Override to tell the compiler that's what you're doing. If you do that to your current code, the compiler will complain that you claim to be overriding a method, but actually you're not. Your IDE might even offer the corrected spelling as a suggestion. If you're using Eclipse, you can even configure it to require the @Override annotation if you want to. I'm sure a quick SO search for "Java override" will turn up lots of similar questions, which may help you out.

MatrixFrog