0

I have the below code and I can't understand why my code doesn't show up menu. If the activity containing data, condition_true = true otherwise condition_true = false. I can clearly see the control going to menutag and the condition is true too. Even I am getting Inflated string too in log. But still menu doesn't show up. I have seen lot of posts on these like link 1, link2 etc., but that didn't solve my problem. Can someone please help me on this issue?

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear(); //Clear view of previous menu
        MenuInflater inflater = getSupportMenuInflater();
        Log.d("menutag", condition_true);
        if(condition_true)
        {
             inflater.inflate(R.layout.menu, menu);
             Log.d("menutag","Inflated");
        }

        return super.onPrepareOptionsMenu(menu); // replaced this with true, but no use.
    }
4

2 回答 2

2

您必须在 xml 中添加选项属性。否则您可以像这样以编程方式执行此操作:

public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu);
    return true;
 }
 public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId){
    case R.id.item1:
       // what you want to do with first button
       break;
    case .....
       break;
    }
    return true;
}
于 2013-07-29T06:22:59.990 回答
0

也许您正在覆盖 onKeyDown 并且它总是返回 true?这可能会导致问题,因为它也会捕获菜单按钮按下事件。

于 2013-07-29T06:50:11.147 回答