Android: java.lang.IllegalArgumentException: Invalid payload item type

polyakovsky picture polyakovsky · Oct 5, 2011 · Viewed 8.5k times · Source

Some users tell me about the exception the got:

java.lang.IllegalArgumentException: Invalid payload item type
at android.util.EventLog.writeEvent(Native Method)
at android.app.Activity.onMenuItemSelected(Activity.java:2452)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:846)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:956)
at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:534)
at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
at android.view.View$PerformClick.run(View.java:11934)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)

But I can't understand what can be wrong. Does anyone have some ideas about the problem? I've tried to repeat that exception, but I failed to do this. Here is the code

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {  
   switch (item.getItemId()) {
   case R.id.about:
      startActivity(new Intent(this, AboutActivity.class));
      return true;
   case R.id.settings:
      startActivity(new Intent(this, SettingsActivity.class));
      return true;
   case R.id.help:
      startActivity(new Intent(this, AboutActivity.class));
      return true;
   }

   return true;
} 

with app_menu xlm file:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/about"
          android:icon="@android:drawable/ic_menu_info_details"
          android:title="@string/about_menu_item"/>
    <item android:id="@+id/settings"
          android:icon="@android:drawable/ic_menu_preferences"
          android:title="@string/settings_menu_item"/>
</menu>

Answer

Mike Venzke picture Mike Venzke · Sep 4, 2014

Like people have said, the bug occurs when there's formatting in the MenuItem title, because of an Android bug in Activity when it writes to the system EventLog.

https://android-review.googlesource.com/#/c/47831/

Although I've only seen it manifest on LG so far, it seems like it will happen in any version of Android before the fix. As far as I can tell from that commit, the earliest release it was tagged in was 4.3, but maybe I'm reading it wrong.

In Activity's onMenuItemSelected, they use MenuItem.getTitleCondensed() which causes the error. I don't use the condensed title anywhere, and as far as I can tell the views that use it by default weren't introduced until v7 support library and we're using v4.

So, my change was to override onMenuItemSelected in a base Activity class and set the condensed title to be a string version of the title. This lets the formatted title be displayed (like with a custom font), and then use the plain string one for event log:

@Override
public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
    // fix android formatted title bug
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 
            && item.getTitleCondensed() != null) {
        item.setTitleCondensed(item.getTitleCondensed().toString());
    }

    return super.onMenuItemSelected(featureId, item);
}

Probably you could just do it in 4.1.2, or just for LG, but it's not clear to me why it hasn't manifest on other versions. It looks like the bug could happen elsewhere. Maybe someone can figure out when it was introduced, but there didn't seem like much downside to needlessly setting an extra string.