选项菜单在ICS及更高版本上崩溃

时间:2012-12-04 13:49:35

标签: android android-menu

选项菜单在Android 操作系统版本 4.0中运行良好。但是当谈到ICS或Jelly bean时,它正在崩溃。请在下面找到我的日志

> 12-04 13:45:05.375: E/AndroidRuntime(5245): FATAL EXCEPTION: main
> 12-04 13:45:05.375: E/AndroidRuntime(5245):
> java.lang.IllegalArgumentException: Invalid payload item type 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> android.util.EventLog.writeEvent(Native Method) 12-04 13:45:05.375:
> E/AndroidRuntime(5245):   at
> android.app.Activity.onMenuItemSelected(Activity.java:2501) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:351)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> android.view.View$PerformClick.run(View.java:13983) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> android.os.Handler.handleCallback(Handler.java:605) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> android.os.Handler.dispatchMessage(Handler.java:92) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> android.os.Looper.loop(Looper.java:137) 12-04 13:45:05.375:
> E/AndroidRuntime(5245):   at
> android.app.ActivityThread.main(ActivityThread.java:4340) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> java.lang.reflect.Method.invokeNative(Native Method) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> java.lang.reflect.Method.invoke(Method.java:511) 12-04 13:45:05.375:
> E/AndroidRuntime(5245):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> 12-04 13:45:05.375: E/AndroidRuntime(5245):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-04
> 13:45:05.375: E/AndroidRuntime(5245):     at
> dalvik.system.NativeStart.main(Native Method)

我正在使用片段,我的选项菜单代码是

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.settings_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.settings:
            Intent intent = new Intent(getActivity(), Settings.class);
            startActivity(intent);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

请让我知道我错过了什么。

2 个答案:

答案 0 :(得分:0)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.settings_menu, menu);

    return true;

}

答案 1 :(得分:0)

你可以尝试另一种方式:---

 static final int SEND = 0;
  static final int DELETE = 1;  
 public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, SEND, 0, "Send").setIcon(R.drawable.send);
    menu.add(0, DELETE, 0, "Delete").setIcon(R.drawable.delete);

    return true;
}   

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case SEND: {
     your code:---
      }
    return true;
      case DELETE: {

    }
    return true;
  }
  return false; 
 }