Android菜单选项不起作用

问题描述

| 我正在执行此菜单,但是它没有更新传感器的速度。 我的代码片段是: 全局变量
private static final int MENU_SLOW = 0;
private static final int MENU_norMAL = 1;
private static final int MENU_FAST = 2;
private static final int MENU_EXIT = -1;
int ROTATION_SPEED = 3;


private static SensorManager mySensorManager;
private boolean sersorrunning;
private myCompassView myCompassView;

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

     myCompassView = (myCompassView)findViewById(R.id.mycompassview);

     mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

     List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

     if(mySensors.size() > 0){

      mySensorManager.registerListener(mySensorEventListener,mySensors.get(0),ROTATION_SPEED);
      sersorrunning = true;
      Toast.makeText(this,\"POINT the Needle to N for norTH\",Toast.LENGTH_LONG).show();

     }
     else{
      Toast.makeText(this,\"COMPASS not Initilised\",Toast.LENGTH_LONG).show();
      sersorrunning = false;
      finish();
     }
 }
传感器速度代码
 if(mySensors.size() > 0){

  mySensorManager.registerListener(mySensorEventListener,ROTATION_SPEED);
  sersorrunning = true;
  Toast.makeText(this,Toast.LENGTH_LONG).show();

 }
 else{
  Toast.makeText(this,Toast.LENGTH_LONG).show();
  sersorrunning = false;
  finish();
 }
这就是我试图更改变量
ROTATION_SPEED
方法
public boolean onoptionsItemSelected (MenuItem item){

    if (item.getItemId() == MENU_SLOW){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_UI;
        Toast.makeText(this,\"Speed changed to SystemPreffered\",Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_norMAL){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_norMAL;
        Toast.makeText(this,\"Speed changed to norMAL\",Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_FAST){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_FASTEST;
        Toast.makeText(this,\"Speed changed to FAST\",Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_EXIT){
        android.os.Process.killProcess(android.os.Process.myPid());
        Toast.makeText(this,\"Exiting application...\",Toast.LENGTH_SHORT).show();
    }
    return false;
}
    

解决方法

        因为您具有预定义的常量,所以不检查“选项”菜单项。它们通常是您在menu / options_menu.xml中定义的资源,并在onCreateOptionsMenu中进行充气。您的处理程序应如下所示:
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle all of the possible menu actions.
    switch (item.getItemId()) {
        case R.id.menu_slow:
            ...
            break;
        case R.id.menu_normal:
                                 ...
            break;
        case R.id.menu_fast:
                                 ...
            break;
        case R.id.menu_exit:
                                 ...
            break;
        }
    Log.e(TAG,\"onOptionsItemSelected\");
    return super.onOptionsItemSelected(item);
在您的处理程序中放入一些日志,以便您也可以查看正在发生的情况。