2012-06-08 56 views
0

菜單沒有顯示在我的emulator.menu代碼是在home.java類中實現的,充氣器使用菜單但是沒有在模擬器上顯示。菜單inflater沒有在模擬器中顯示

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater Inflater = getMenuInflater(); 
    Inflater.inflate(R.layout.menu, menu); 
    return(super.onCreateOptionsMenu(menu)); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == R.id.logout) { 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.remove("logged"); 
     editor.commit(); 
     finish(); 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

回答

1

正如Vipul Shah說,你應該改變這一行:

Inflater.inflate(R.layout.menu, menu); 

Inflater.inflate(R.menu.menu, menu); 

之後 - 更換您的menu.xmlres/menu,而不是res/layout

那麼你顯然應該返回true而不是返回super方法的結果。 您的代碼應該是這樣的:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater Inflater = getMenuInflater(); 
    Inflater.inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.logout : 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.remove("logged"); 
     editor.commit(); 
     finish(); 
     return true; 
    default: return super.onOptionsItemSelected(item); 
} 
+1

thnx其工作正常.. :) – shitanshu

0

更改此

Inflater.inflate(R.layout.menu, menu); 

爲了這

Inflater.inflate(R.menu.menu, menu); 

把你menu.xml文件中res /菜單文件夾。