菜单按钮不起作用

时间:2010-07-20 04:44:16

标签: java android override

我想在用户按下菜单按钮时添加一些选项,我尝试了以下操作但是当我运行应用程序并按下菜单按钮时似乎没有发生任何事情:

菜单xml文件

<?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
         <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>

MapAcitivity类

    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;
    }
}

1 个答案:

答案 0 :(得分:3)

简短回答:onCreateOptionsMenu必须以小写o开头,而不是大写O.

更长的答案:在Java中,方法名称始终以小写字母开头(按照惯例,不是因为Java编译器强制执行),Android忠实地遵循约定。您打算覆盖Activity.onCreateOptionsMenu,但实际上创建了一种全新的方法MyMapActivity.OnCreateOptionsMenu。当然,你可以创建你想要的任何方法,所以编译器什么也没说。无论何时打算override a method from a superclass,都应该用@Override标记它,告诉编译器你正在做什么。如果您对当前代码执行此操作,编译器将抱怨您声称重写方法,但实际上您不是。您的IDE甚至可能会提供更正后的拼写作为建议。如果您正在使用Eclipse,您甚至可以根据需要将其配置为 require @Override注释。我确信快速搜索“Java覆盖”会发现很多类似的问题,这可能对你有帮助。