Monday, February 18, 2013

Solution: Invalid payload item type onMenuItemSelected

If you're developing for Android 2.3 or lower, users can reveal the options menu panel by pressing the Menu button.
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated.

You have a sample code in 2.3, which is working fine for MENU option.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sample, menu);
        return true;
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


But there is a following exception when you have run same code in 4.0+ version

java.lang.IllegalArgumentException: Invalid payload item type
at android.util.EventLog.writeEvent(Native Method)
at android.app.Activity.onMenuItemSelected(Activity.java:2565)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:973)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:309)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1099)
at android.widget.ListView.performItemClick(ListView.java:4758)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2556)
at android.widget.AbsListView$1.run(AbsListView.java:3223)
at android.os.Handler.handleCallback(Handler.java:608)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Solution: 
Override onMenuItemSelected, and use return true or false than super.onMenuItemSelected(featureId, item).

2 comments :

  1. But onMenuItemSelected is a final method so cannot be overriden?

    ReplyDelete
    Replies
    1. which activity your extending? ActionBarActivity made final to onMenuItemSelected but check the Activity it is not final. may be your using some activity which made onMenuItemSelected has final.

      Delete