Android:java.lang.IllegalArgumentException:无效的有效负载项类型

发布于 2024-12-08 09:46:31 字数 2399 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

没有伤那来痛 2024-12-15 09:46:31

正如人们所说,当 MenuItem 标题中有格式时,就会出现该错误,因为 Activity 写入系统 EventLog 时存在 Android 错误。

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

虽然我'到目前为止,我们只在 LG 上看到过这个问题,似乎在修复之前的任何 Android 版本中都会发生这种情况。据我从该提交中可以看出,它被标记的最早版本是 4.3,但也许我读错了。

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

因此,我的更改是重写基 Activity 类中的 onMenuItemSelected 并将压缩标题设置为标题的字符串版本。这可以显示格式化的标题(就像使用自定义字体一样),然后使用纯字符串作为事件日志:

@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,但我不清楚为什么要这样做在其他版本上没有体现。看起来这个错误可能发生在其他地方。也许有人可以弄清楚它是何时引入的,但不必要地设置额外的字符串似乎并没有太大的缺点。

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.

听你说爱我 2024-12-15 09:46:31

对于使用 AppCompat 的用户:

您无法覆盖 Activity.onMenuItemSelected()。如果您需要的只是将格式应用于 MenuItemtitle 并且您不关心 titleCondensed

    CharSequence rawTitle = "Click here";
    menuItem.setTitleCondensed(rawTitle);

    SpannableString spannableTitle = new SpannableString(rawTitle);
    //...whatever formatting on spannableTitle, you want
    menuItem.setTitle(spannableTitle);

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);
情深如许 2024-12-15 09:46:31

对我来说,此错误仅发生在 ActionBar 标题/副标题中的自定义字体 SpannableString 中。删除自定义格式解决了问题。

黑客(对不起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));
}

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));
}
念﹏祤嫣 2024-12-15 09:46:31

我也有同样的问题。原来我正在尝试格式化我的字符串。

    <string name="send">
        <b>Send</b>
    </string>

我将其更改为:

    <string name="send">
        Send
    </string>

希望这会有所帮助。

您也可以使用 CDATA 标签,这里还有一个相关的问题链接

        <string name="send">
             <![CDATA[<b>Send</b>]]>
        </string>

感谢特拉维斯指出了这一点。

I had the same problem too. It turned out that I was trying to format my strings.

    <string name="send">
        <b>Send</b>
    </string>

I changed it to:

    <string name="send">
        Send
    </string>

I hope this helps.

You can use CDATA tags also here is a related question link.

        <string name="send">
             <![CDATA[<b>Send</b>]]>
        </string>

Thanks to Travis for pointing this out.

生生漫 2024-12-15 09:46:31

我也遇到了同样的问题,但是查看Android源代码,我发现问题发生在写入内部日志时,无法打印格式化文本。

解决方案:只需通过重新实现此函数来跳过此日志,并且不要称其为超级!

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    ... // Do your staff
    return true;
}

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!!

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    ... // Do your staff
    return true;
}
不离久伴 2024-12-15 09:46:31

我遇到了同样的问题

Android 4.1 中菜单项的问题是字符串

最初,我的菜单项是这样的:

<item android:id="@+id/item1" android:title="@string/ic_login" 
   android:icon="@drawable/ic_login" android:orderInCategory="100" >
</item>

但这不起作用。

我把它改成这样:

<item android:id="@+id/item1"
    android:title="Login"
    android:orderInCategory="100"
    android:icon="@drawable/ic_login"
/>

效果很好。

I had the same problem

The problem in Android 4.1 with Menu Item is the Strings

Originally, my Menu Item was like this:

<item android:id="@+id/item1" android:title="@string/ic_login" 
   android:icon="@drawable/ic_login" android:orderInCategory="100" >
</item>

and that didn't work.

I change it into this:

<item android:id="@+id/item1"
    android:title="Login"
    android:orderInCategory="100"
    android:icon="@drawable/ic_login"
/>

and that worked well.

月隐月明月朦胧 2024-12-15 09:46:31

onMenuItemSelected(...) 有一个“不是很好”的想法,

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (item.getTitle() instanceof SpannableString) {
        SpannableString sp = (SpannableString)item.getTitle();
        Object[] spans = sp.getSpans(0, sp.length(), Object.class);
        if (spans != null && spans.length > 0) {
            // set text without span markups, need for super.onMenuItemSelected(featureId, item);
            item.setTitleCondensed(sp.toString());
            boolean result = super.onMenuItemSelected(featureId, item);
            item.setTitleCondensed(sp);
            return result;
        }
    }


    return super.onMenuItemSelected(featureId, item);
}

它的解决方法是

设置原始字符串、克服有问题的地方、设置原始格式化字符串。
我认为在此示例中格式化字符串是一个 SpannableString 对象,您可能会使用其他东西

there is a "not very good' idea with onMenuItemSelected(...)

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (item.getTitle() instanceof SpannableString) {
        SpannableString sp = (SpannableString)item.getTitle();
        Object[] spans = sp.getSpans(0, sp.length(), Object.class);
        if (spans != null && spans.length > 0) {
            // set text without span markups, need for super.onMenuItemSelected(featureId, item);
            item.setTitleCondensed(sp.toString());
            boolean result = super.onMenuItemSelected(featureId, item);
            item.setTitleCondensed(sp);
            return result;
        }
    }


    return super.onMenuItemSelected(featureId, item);
}

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

许你一世情深 2024-12-15 09:46:31

我找到了如何导致此错误。
在膨胀菜单中,我设置了这样的标题

menu.setTitle(Html.fromHtml("Menu line #1<br>And what is displayed on line #2"));

当我使用此设置时,它将导致无效负载异常。然后我用

menu.setTitle("Menu line #1. Opps, can not set what is displayed on line #2");

它就可以正常工作了,就像我刚认识 Android 的时候一样。
我希望我的菜单有 2 行,因此使用 html 标签来换行,但仅在 Activity 上成功,但在另一个 Activity 上失败。我不知道发生了什么事。有人有其他想法或解决方案吗?

I found how to cause this error.
In inflate menu, I set title like that

menu.setTitle(Html.fromHtml("Menu line #1<br>And what is displayed on line #2"));

When I use this setting, it will cause invalid payload exception. Then I use

menu.setTitle("Menu line #1. Opps, can not set what is displayed on line #2");

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?

月朦胧 2024-12-15 09:46:31

对于在支持库下使用带有 DrawerLayout 的工具栏的任何人,他们也可能会出现此问题。这个问题可以通过覆盖默认的导航点击实现来解决。

@Override
public void setSupportActionBar(Toolbar toolbar) {
    super.setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDrawer();
        }
    });
}

这应该有效。

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.

@Override
public void setSupportActionBar(Toolbar toolbar) {
    super.setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDrawer();
        }
    });
}

This should work.

2024-12-15 09:46:31

我遇到了同样的问题

问题是在 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.

执着的年纪 2024-12-15 09:46:31

在调用 setDisplayHomeAsUp() 后调用 setSupportActionBar() 似乎也会导致此问题。建议检查多个 setSupportActionBar() 调用,尤其是在基类中(如果存在)。

删除对 setSupportActionBar() 的意外调用后,问题就消失了。

Calling setSupportActionBar() after calling setDisplayHomeAsUp() also seems to cause this issue. It is recommended to check for multiple setSupportActionBar() calls, especially in the base classes if they exist.

On deleting the unintended call to setSupportActionBar(), the problem went away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文