3

我正在尝试以编程方式设置菜单,我的方法将 com.actionbarsherlock.view.Menu 菜单作为参数。当我想更改菜单项的样式时,我正在尝试设置 SpannableString 可跨越并将其传递给添加。menu.add(0, MENU_ABOUT, 0, spannable); 当我点击该菜单项时,这只会在 ICS 中导致崩溃。如果我遗漏了什么,有什么想法。谢谢 这是我正在尝试的代码,当我运行应用程序并选择“关于应用程序”时它崩溃了。仅供参考,我使用 SpannableString 是因为我想为菜单项添加一些样式和颜色。如果我使用常规字符串,则不会发生崩溃,但是我无法进行样式设置

    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu)
{
    SpannableString spannable = new SpannableString("About Application");
    menu.add(0, MENU_ABOUT, 0, spannable);
    return true;
}

这是堆栈跟踪:

    09-05 02:25:32.849: E/AndroidRuntime(684): FATAL EXCEPTION: main
    09-05 02:25:32.849: E/AndroidRuntime(684): java.lang.IllegalArgumentException:  Invalid payload item type
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.util.EventLog.writeEvent(Native Method)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.app.Activity.onMenuItemSelected(Activity.java:2501)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:351)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3180)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.View.dispatchTouchEvent(View.java:5541)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.View.dispatchPointerEvent(View.java:5721)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.os.Looper.loop(Looper.java:137)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at android.app.ActivityThread.main(ActivityThread.java:4424)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at java.lang.reflect.Method.invoke(Method.java:511)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    09-05 02:25:32.849: E/AndroidRuntime(684):  at dalvik.system.NativeStart.main(Native Method)

谢谢

4

1 回答 1

1

你能发布堆栈跟踪和可能的来源吗?(我会对此发表评论,但在发布时我没有 50 个代表)

在黑暗中刺伤......你可能会getMenuInflater()在应该打电话的时候打电话getSupportMenuInflater()

/编辑

在尝试复制您的错误后,我注意到设置 ActionBar 的 ActionButton 文本的样式似乎不可行。即使不使用 Sherlock 图书馆。在运行低于 API 11 的任何设备上,不会引发错误,但也没有设置按钮样式。

您可能必须创建一个带有样式的静态 xml 文件,正如这个答案所建议的那样 以及它下面的一个(只是一个想法)

于 2012-09-05T01:41:15.000 回答