3

正如问题所说,它不起作用。当我按下菜单按钮时,什么也没有出现。

这是菜单活动:

public class AndroidMenuActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewphoto);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
     super.onCreateOptionsMenu(menu); 
     MenuInflater menuInflater = getMenuInflater();
     menuInflater.inflate(R.menu.menu, menu);
     return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
        case R.id.edit:
            Toast.makeText(getApplicationContext(), "edit selected", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(getApplicationContext(), PhotoFlashActivity.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

和一个非常小的 menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/edit"
    android:title="Edit"/>   

</menu>

我不知道出了什么问题,我看过的教程没有提到将活动添加到清单文件中,所以我不确定是否必须这样做。即使我这样做了,它也不起作用。我试过清理项目,但这也不起作用。

4

5 回答 5

0
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);        
        menuInflater.inflate(R.menu.menu, menu);
        menu.findItem(R.id.edit).setTitle("Edit");
}

您也可以尝试强制:

menu.findItem(R.id.edit).setVisible(true);

然后...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.edit:
            //DO SOMETHING
            return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

或者....您可以在 java 上创建菜单:

private static final int MENU_EDIT = 1;    
MenuItem itemconfig = menu.add(0, MENU_EDIT , 0, "Edit");
            itemconfig.setIcon(android.R.drawable.ic_menu_edit);
            itemconfig.setShowAsAction(2);

和 onOptionsItemSelected:

case: MENU_EDIT
     //DO SOMETHING
     return true;
于 2014-01-28T12:51:45.397 回答
0

试试这个代码,它对works我来说很完美。

主类

public class OptionmenuActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {


    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    //return super.onCreateOptionsMenu(menu);

    menu.add("hello").setIcon(R.drawable.ic_launcher);
    menu.add("patel").setIcon(R.drawable.ic_launcher);
    menu.add("abc").setIcon(R.drawable.ic_launcher);
    menu.add("hello").setIcon(R.drawable.ic_launcher);
    menu.add("").setIcon(R.drawable.ic_launcher);
    menu.add("").setIcon(R.drawable.ic_launcher);

    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.feeds:
        Toast msg = Toast.makeText(OptionmenuActivity.this, "feeds", Toast.LENGTH_LONG);
        msg.show();
        return true;

    case R.id.friends:
        Toast msg1 = Toast.makeText(OptionmenuActivity.this, "friends", Toast.LENGTH_LONG);
        msg1.show();
        return true;
    case R.id.about:
        Toast msg2 = Toast.makeText(OptionmenuActivity.this, "about", Toast.LENGTH_LONG);
        msg2.show();
        return true;

    case 2:

         Toast msg3 = Toast.makeText(OptionmenuActivity.this, "Menu 1", Toast.LENGTH_LONG);
            msg3.show();
            return true;

    }
    return true;
}
}

在创建内部文件夹后将文件夹创建menu到文件夹中。resmenu.xmlmenu

菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/feeds"
    android:title="Feeds"
    android:icon="@drawable/ic_launcher"/>

<item
    android:id="@+id/friends"
    android:title="Friends"/>

<item
    android:id="@+id/about"
    android:title="About"/>

于 2012-08-10T04:16:02.383 回答
0

这可能没什么,但是如果您将 onCreateOptionsMenu(...) 重写为如下所示会发生什么:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

     MenuInflater menuInflater = getMenuInflater();
     menuInflater.inflate(R.menu.menu, menu);
     return super.onCreateOptionsMenu(menu); 
}
于 2012-08-10T03:12:14.690 回答
-1

像这样更改您的代码并检查。

     @Override
 public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
 super.onCreateOptionsMenu(menu); 
 MenuInflater menuInflater = getMenuInflater();
 menuInflater.inflate(R.menu.menu, menu);
 return true;
}

 public boolean onContextItemSelected(MenuItem item) {       
    onOptionsItemSelected(item);
    return true;
}

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // TODO Auto-generated method stub
  switch(item.getItemId()){
    case R.id.edit:
        Toast.makeText(getApplicationContext(), "edit selected", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), PhotoFlashActivity.class);
        startActivity(i);
        return true;
        default:
         return super.onOptionsItemSelected(item);
   }
 }
}
于 2012-08-10T03:40:08.923 回答
-1
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_custom__list_view, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {


                ....code......

            return super.onOptionsItemSelected(item);
   }
于 2012-08-10T03:49:35.797 回答