Android:java.lang.IllegalArgumentException:无效的有效载荷项目类型 [英] Android: java.lang.IllegalArgumentException: Invalid payload item type

查看:31
本文介绍了Android:java.lang.IllegalArgumentException:无效的有效载荷项目类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些用户告诉我得到的异常:

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;
} 

使用 app_menu xlm 文件:

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>

推荐答案

正如人们所说,由于 Activity 在写入系统 EventLog 时存在 Android 错误,因此在 MenuItem 标题中有格式时会出现该错误.

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/

虽然到目前为止我只看到它在 LG 上出现,但在修复之前它似乎会在任何版本的 Android 中发生.据我从那次提交中可以看出,它被标记的最早版本是 4.3,但也许我读错了.

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.

在 Activity 的 onMenuItemSelected 中,它们使用了导致错误的 MenuItem.getTitleCondensed().我不会在任何地方使用缩写标题,据我所知,默认情况下使用它的视图直到 v7 支持库才被引入,我们正在使用 v4.

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.

因此,我的更改是在基本 Activity 类中覆盖 onMenuItemSelected 并将精简标题设置为标题的字符串版本.这允许显示格式化的标题(就像使用自定义字体一样),然后使用纯字符串之一作为事件日志:

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);
}

也许你可以只在 4.1.2 中这样做,或者只为 LG 做,但我不清楚为什么它没有出现在其他版本中.看起来这个错误可能发生在其他地方.也许有人可以弄清楚它是什么时候引入的,但是不必要地设置额外的字符串似乎没有太大的缺点.

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.

这篇关于Android:java.lang.IllegalArgumentException:无效的有效载荷项目类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆