IllegalArgumentException无效的有效内容项类型 - 使用ActionBarCompat

时间:2013-10-27 12:13:46

标签: android menuitem android-actionbar-compat

Logcat:http://crashes.to/s/72388b92dd7

我使用动作栏compat,因此我无法覆盖'onMenuItemSelected',因为它是最终方法。似乎只发生在某些人身上?

我已经在'onOptionsItemSelected'上返回true,但它没有帮助,同时提交了一份错误报告,希望有人会看到这个。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         if (mDrawerToggle.onOptionsItemSelected(item)) {


             return true;
           }
        switch (item.getItemId()) {                      
            case R.id.menu_add_user:
                Intent intent = new Intent(this, Add_User.class);

                startActivity(intent);
                return true;
            case R.id.menu_about:
                buildAboutDialog();
                return true;
            case R.id.menu_chat:

                return true;
            case R.id.menu_feedback:
                buildFeedbackDialog("a");
                return true;
            case R.id.menu_show_images:
                if(appPrefs.getBoolean("showImages", false) == true){
                    appPrefs.saveBoolean("showImages", false);
                    MenuShowImages.setTitle("Hide images");
                    MenuShowImages.setIcon(R.drawable.navigation_cancel);
                }
                else
                {
                    appPrefs.saveBoolean("showImages", true);
                    MenuShowImages.setTitle("Show images");
                    MenuShowImages.setIcon(R.drawable.navigation_accept);
                }
                Intent intentab = getIntent();
                overridePendingTransition(0, 0);
                intentab.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                finish();

                overridePendingTransition(0, 0);
                startActivity(intentab);
                return true;
            case R.id.menu_news:
                Intent intent_news = new Intent(this, NewsActivity.class);
                startActivity(intent_news);
                return true;
            case R.id.menu_horny:

                if(appPrefs.getBoolean("Adult", false) == true){
                    Hornyitem.setTitle("Filter Normal");
                }
                else
                {
                    Hornyitem.setTitle("Filter Adult");
                }

                Intent intentaa = getIntent();
                overridePendingTransition(0, 0);
                intentaa.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                finish();

                overridePendingTransition(0, 0);
                startActivity(intentaa);

                return true;

        }

        return true;
    }

menu.xml:

<menu xmlns:yourapp="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     >

    <item
        android:id="@+id/menu_add_user"
        android:orderInCategory="0"
        yourapp:showAsAction="ifRoom" android:icon="@drawable/social_add_person" 
        android:title="@string/menu_add_user"/>
    <item 
        android:id="@+id/menu_overflow" 
        android:icon="@drawable/ic_menu_moreoverflow_normal_holo_dark"
        yourapp:showAsAction="always" android:orderInCategory="1" 
        android:title="Show menu">
        <menu>
             <item 
        android:id="@+id/menu_about" 
        android:icon="@drawable/action_about"
        yourapp:showAsAction="ifRoom" 
        android:title="@string/menu_help" 
        android:orderInCategory="1"/>
     <item 
        android:id="@+id/menu_feedback" 
        android:icon="@drawable/social_chat"
        yourapp:showAsAction="ifRoom" 
        android:title="Request feature" android:orderInCategory="1" android:visible="false"/>
     <item 
        android:id="@+id/menu_show_images" 
        android:icon="@drawable/navigation_accept"
        yourapp:showAsAction="ifRoom" 
        android:title="@string/show_images" android:orderInCategory="1"/>

     <item 
        android:id="@+id/menu_noads" 
        android:icon="@drawable/device_access_accounts"
        yourapp:showAsAction="ifRoom" 
        android:title="Remove ads" android:orderInCategory="1" android:visible="false"/>

     <item 
        android:id="@+id/menu_news" 
        android:icon="@drawable/location_web_site"
        yourapp:showAsAction="ifRoom" 
        android:title="@string/menu_news" android:orderInCategory="1" android:visible="false"/>
     <item 
        android:id="@+id/menu_chat" 
        android:icon="@drawable/location_web_site"
        yourapp:showAsAction="ifRoom" 
        android:title="Chatroom" android:orderInCategory="1" android:visible="false"/>

    </menu>
</item>
</menu>

的字符串:

    <string name="title_activity_news">BroakenMedia News</string>
    <string name="title_activity_guide">User Guide</string>
    <string name="title_activity_details">User Details</string>
    <string name="title_activity_change_background">Change Background</string>
    <string name="action_settings">Settings</string>
    <string name="hide_images">Hide images</string>
    <string name="show_images">Show images</string>
    <string name="filter_adult_18">Filter Adult (18+)</string>
    <string name="filter_normal">Filter Normal</string>
    <string name="dialog_about">About</string>
    <string name="action_bar_title_norm">Users</string>
    <string name="menu_news">News</string>
    <string name="menu_help">Help</string>
    <string name="menu_add_user">Add sser</string>

actionbar settitle:

String mystring = getString(R.string.action_bar_title_norm);
        SpannableString s = new SpannableString(mystring);
        s.setSpan(new TypefaceSpan(this, "cubano"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

actionBar.setTitle(s);

1 个答案:

答案 0 :(得分:2)

对我来说,只有ActionBar标题中的自定义字体SpannableString才会出现此异常。它仅出现在LG设备中。所以我这样编码,

public static void setActionBarTitle(ActionBarActivity activity, String title) {
    SpannableString spannableTitle = new SpannableString(title);
    spannableTitle.setSpan(new TypefaceSpan(activity, "CustomFont.ttf"), 0, title.length(),    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ActionBar actionBar = activity.getSupportActionBar();
    actionBar.setTitle(isLGDevice() ? title : spannableTitle);
}

public static boolean isLGDevice() {    
    return (Build.MANUFACTURER.contains("LG") || Build.MODEL.contains("LG"));
}