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

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


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:

@Overridepublic 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.


For those using AppCompat:

you can't override Activity.onMenuItemSelected(). If all you need is to apply formatting to MenuItem's title and you don't care about titleCondensed:

    CharSequence rawTitle = "Click here";    menuItem.setTitleCondensed(rawTitle);    SpannableString spannableTitle = new SpannableString(rawTitle);    //...whatever formatting on spannableTitle, you want    menuItem.setTitle(spannableTitle);


For me this error occurred only for a custom font SpannableString in ActionBar title / subtitle. Removing custom formatting solved the problem.

Hack (sorry LG ;-) :

public static void setActionBarTitle(ActionBarActivity a, String s) {    SpannableString ss = new SpannableString(s);    ss.setSpan(new TypefaceSpan(a, "Roboto-Light.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    ActionBar actionBar = a.getSupportActionBar();    actionBar.setDisplayShowTitleEnabled(true);    actionBar.setLogo(R.drawable.icon);    actionBar.setTitle(isManufacturer("LG") ? s : ss);}public static boolean isManufacturer(String company) {    String manufacturer = Build.MANUFACTURER;    String model = Build.MODEL;    return (manufacturer.contains(company) || model.contains(company));}