我正在尝试以编程方式设置菜单,我的方法将com.actionbarsherlock.view.Menu菜单作为参数。因为我想改变菜单项的样式,我试图设置一个SpannableString spannable并传递给它添加。 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)
谢谢
答案 0 :(得分:1)
您可以发布堆栈跟踪和可能的来源吗? (我会将此作为评论,但在发布时我没有50的代表)
在黑暗中刺伤......
当您应该致电getMenuInflater()
getSupportMenuInflater()
/编辑
在尝试复制你的错误后,我注意到ActionBar的ActionButton文本样式似乎不可行。即使不使用Sherlock图书馆。在运行API 11以下任何设备的设备上,未抛出错误,但按钮也没有设置样式。
您可能必须使用样式创建一个静态xml文件Answer suggests 和one below it(只是一个想法)