更改项目颜色后,项目选择菜单不起作用

时间:2014-01-07 20:38:20

标签: android menuitem

我遇到了onOptionsItemSelected的问题。点击项目后出现错误。 它在更改项目中文本的颜色后发生。

请帮忙。我做错了什么?

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        MenuItem menuItem = menu.getItem(0);
        CharSequence menuTitle = menuItem.getTitle();
        SpannableString styledMenuTitle = new SpannableString(menuTitle);
        styledMenuTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 0, styledMenuTitle.length(), 0);
        menuItem.setTitle(styledMenuTitle);

return super.onPrepareOptionsMenu(menu);             
}


public boolean onOptionsItemSelected( MenuItem item) {

          if (item.getItemId() == R.id.action_exit) {  
             this.finish();                        
          }  
        return true;  
}       
}

日志

01-07 22:37:07.060: ERROR/AndroidRuntime(9757): FATAL EXCEPTION: main
01-07 22:37:07.060: ERROR/AndroidRuntime(9757): java.lang.IllegalArgumentException: Invalid payload item type
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.util.EventLog.writeEvent(Native Method)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.app.Activity.onMenuItemSelected(Activity.java:2204)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:750)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.view.View$PerformClick.run(View.java:9080)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.os.Handler.handleCallback(Handler.java:587)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.os.Looper.loop(Looper.java:130)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at android.app.ActivityThread.main(ActivityThread.java:3822)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at java.lang.reflect.Method.invoke(Method.java:507)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 22:37:07.060: ERROR/AndroidRuntime(9757):     at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助!

0 个答案:

没有答案