Android:java.lang.IllegalArgumentException:无效的有效负载项类型
一些用户告诉我所得到的异常:
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)
但我不明白可能出了什么问题。有人对这个问题有一些想法吗?我试图重复这个异常,但我失败了。这是 app_menu xlm 文件的代码
@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;
}
:
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
正如人们所说,当 MenuItem 标题中有格式时,就会出现该错误,因为 Activity 写入系统 EventLog 时存在 Android 错误。
https://android-review.googlesource.com/#/c/47831/
虽然我'到目前为止,我们只在 LG 上看到过这个问题,似乎在修复之前的任何 Android 版本中都会发生这种情况。据我从该提交中可以看出,它被标记的最早版本是 4.3,但也许我读错了。
在 Activity 的 onMenuItemSelected 中,他们使用 MenuItem.getTitleCondensed() 导致错误。我不会在任何地方使用压缩标题,据我所知,默认情况下使用它的视图直到 v7 支持库才被引入,而我们正在使用 v4。
因此,我的更改是重写基 Activity 类中的 onMenuItemSelected 并将压缩标题设置为标题的字符串版本。这可以显示格式化的标题(就像使用自定义字体一样),然后使用纯字符串作为事件日志:
也许您可以在 4.1.2 中执行此操作,或者仅适用于 LG,但我不清楚为什么要这样做在其他版本上没有体现。看起来这个错误可能发生在其他地方。也许有人可以弄清楚它是何时引入的,但不必要地设置额外的字符串似乎并没有太大的缺点。
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:
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.
对于使用
AppCompat
的用户:您无法覆盖
Activity.onMenuItemSelected()
。如果您需要的只是将格式应用于MenuItem
的title
并且您不关心titleCondensed
:For those using
AppCompat
:you can't override
Activity.onMenuItemSelected()
. If all you need is to apply formatting toMenuItem
'stitle
and you don't care abouttitleCondensed
:对我来说,此错误仅发生在 ActionBar 标题/副标题中的自定义字体 SpannableString 中。删除自定义格式解决了问题。
黑客(对不起LG;-):
For me this error occurred only for a custom font SpannableString in ActionBar title / subtitle. Removing custom formatting solved the problem.
Hack (sorry LG ;-) :
我也有同样的问题。原来我正在尝试格式化我的字符串。
我将其更改为:
希望这会有所帮助。
您也可以使用 CDATA 标签,这里还有一个相关的问题链接。
感谢特拉维斯指出了这一点。
I had the same problem too. It turned out that I was trying to format my strings.
I changed it to:
I hope this helps.
You can use CDATA tags also here is a related question link.
Thanks to Travis for pointing this out.
我也遇到了同样的问题,但是查看Android源代码,我发现问题发生在写入内部日志时,无法打印格式化文本。
解决方案:只需通过重新实现此函数来跳过此日志,并且不要称其为超级!
I had the same issue, but looking the Android Source Code, I found that the problem occurs when writting an internal Log, which can't print a formatted text.
The solution: simply skip this Log by reimplementing this function and DON'T CALL ITS SUPER!!
我遇到了同样的问题
Android 4.1 中菜单项的问题是字符串
最初,我的菜单项是这样的:
但这不起作用。
我把它改成这样:
效果很好。
I had the same problem
The problem in Android 4.1 with Menu Item is the Strings
Originally, my Menu Item was like this:
and that didn't work.
I change it into this:
and that worked well.
onMenuItemSelected(...) 有一个“不是很好”的想法,
它的解决方法是
设置原始字符串、克服有问题的地方、设置原始格式化字符串。
我认为在此示例中格式化字符串是一个 SpannableString 对象,您可能会使用其他东西
there is a "not very good' idea with onMenuItemSelected(...)
It's workaround actions like
to set a raw string, overcame a buggy place, set original formatted string back.
I concider that formatted string is a SpannableString object in this example, may be you'll use something else
我找到了如何导致此错误。
在膨胀菜单中,我设置了这样的标题
当我使用此设置时,它将导致无效负载异常。然后我用
它就可以正常工作了,就像我刚认识 Android 的时候一样。
我希望我的菜单有 2 行,因此使用 html 标签来换行,但仅在 Activity 上成功,但在另一个 Activity 上失败。我不知道发生了什么事。有人有其他想法或解决方案吗?
I found how to cause this error.
In inflate menu, I set title like that
When I use this setting, it will cause invalid payload exception. Then I use
it work fine as when I known Android.
I want my menu have 2 lines, so use html tag to break line, but success on only Activity, but falied on another. I don't know what happend. Anyone have another idea or solution?
对于在支持库下使用带有 DrawerLayout 的工具栏的任何人,他们也可能会出现此问题。这个问题可以通过覆盖默认的导航点击实现来解决。
这应该有效。
For anyone using the Toolbar with DrawerLayout under Support Library, this issue might occur for them too. This problem can be solved by overriding the default navigation click implementation.
This should work.
我遇到了同样的问题
问题是在 Android 4.1 中,菜单项无法格式化字符串
strings.xml 中的原始字符串在字符串中使用粗体标记,但这不起作用。然后我删除了粗体标签,效果很好。
I had the same problem
The problem is in Android 4.1 the Menu Item can't be formated strings
The original string in the strings.xml was using Bold tag in the string and that didn't work. Then I removed the Bold tag and that worked well.
在调用
setDisplayHomeAsUp()
后调用setSupportActionBar()
似乎也会导致此问题。建议检查多个setSupportActionBar()
调用,尤其是在基类中(如果存在)。删除对
setSupportActionBar()
的意外调用后,问题就消失了。Calling
setSupportActionBar()
after callingsetDisplayHomeAsUp()
also seems to cause this issue. It is recommended to check for multiplesetSupportActionBar()
calls, especially in the base classes if they exist.On deleting the unintended call to
setSupportActionBar()
, the problem went away.