,我用的菜單爲活動在Android的菜單並沒有在Android應用程序正常工作
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/logout"
android:title="Logout" />
<item
android:id="@+id/compose"
android:title="Compose" />
<item
android:id="@+id/refresh"
android:title="Refresh" />
</menu>
和活動:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.inboxmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.logout:
SharedPreferences settings =getSharedPreferences("CASPreferences", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("uid", "");
prefEditor.putString("stdno", "");
prefEditor.putString("firstname", "");
prefEditor.putString("lastname", "");
prefEditor.putString("lastname", "");
prefEditor.commit();
Intent myIntent = new Intent(this, LoginActivity.class);
startActivity(myIntent);
return true;
case R.id.refresh:
Log.e("menu" , "refresh");
//Intent intent=new Intent(this,MainActivity.class);
startActivity(getIntent());
finish();
case R.id.compose:
Log.e("menu" , "compose");
Intent intent1=new Intent(this,ComposeActivity.class);
startActivity(intent1);
default:
return super.onOptionsItemSelected(item);
}
}
,但是當我點擊刷新按鈕,它會撰寫活動!就像你點擊撰寫。
爲什麼會發生這種情況?
請注意'break'語句,特別是! – Dave