2011-06-13 72 views
-1

我正在執行此菜單的過程中,但它沒有更新我的傳感器的速度。 我的一小段代碼片段是: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, 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(); 
} 

這裏是如何我試圖改變變量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; 
} 

回答

0

選項菜單項沒有被檢查,因爲你有預定義的co nstants。它們通常是您在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); 

在處理程序中放入幾個日誌,以便您可以看到發生了什麼。

+0

謝謝John fragans – hisheeraz 2011-06-19 05:44:16