使用AppCompact使Android 4导致类android.support.v7.internal.view.menu.ActionMenuItemView出错

时间:2015-03-03 07:41:28

标签: java android android-toolbar

每次我在Android 4上运行应用程序(在Android 4.4.4上运行正常)我都会遇到以下异常

Error inflating class android.support.v7.internal.view.menu.ActionMenuItemView

TextView.java line 918 android.widget.TextView.<init>

完整的崩溃日志是:

android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.internal.view.menu.ActionMenuItemView
   at android.view.LayoutInflater.createView(LayoutInflater.java:613)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.createItemView(BaseMenuPresenter.java:161)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:180)
   at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:170)
   at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:429)
   at android.support.v7.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1129)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91)
   at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:207)
   at android.support.v7.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:279)
   at android.support.v7.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1021)
   at android.support.v7.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1044)
   at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:465)
   at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4921)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.createItemView(BaseMenuPresenter.java:161)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:180)
   at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:170)
   at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:429)
   at android.support.v7.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1129)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91)
   at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:207)
   at android.support.v7.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:279)
   at android.support.v7.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1021)
   at android.support.v7.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1044)
   at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:465)
   at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4921)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f010026 a=-1}
   at android.content.res.Resources.loadColorStateList(Resources.java:2068)
   at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
   at android.widget.TextView.<init>(TextView.java:918)
   at android.support.v7.internal.widget.CompatTextView.<init>(CompatTextView.java:45)
   at android.support.v7.internal.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:76)
   at android.support.v7.internal.view.menu.ActionMenuItemView.<init>(ActionMenuItemView.java:72)
   at java.lang.reflect.Constructor.constructNative(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.createItemView(BaseMenuPresenter.java:161)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:180)
   at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:170)
   at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:429)
   at android.support.v7.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1129)
   at android.support.v7.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91)
   at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:207)
   at android.support.v7.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:279)
   at android.support.v7.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1021)
   at android.support.v7.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1044)
   at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:465)
   at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4921)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   at dalvik.system.NativeStart.main(NativeStart.java)

菜单XML如下:

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

<item
    android:id="@+id/action_search"
    android:icon="@drawable/action_search"
    android:orderInCategory="2"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"/>
<item
    android:id="@+id/action_pending_invites"
    android:icon="@drawable/ic_action_social_notifications"
    android:orderInCategory="3"
    android:title="@string/pending_invites"
    app:showAsAction="always"/>
<item
    android:id="@+id/action_sync"
    android:icon="@drawable/stat_notify_sync"
    android:orderInCategory="4"
    android:title="@string/action_sync"
    app:showAsAction="ifRoom|withText"/>
<item
    android:id="@+id/action_global_settings"
    android:icon="@drawable/action_search"
    android:orderInCategory="100"
    android:title="@string/settings"
    app:showAsAction="ifRoom|withText"/>
</menu>

我的活动的布局是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeScreen" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxxxxxxxxx" />

    <ListView
        android:id="@+id/listViewHomeScreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/adview" >
    </ListView>

    <ProgressBar
        android:id="@+id/progressBarHomeScreen"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

    <TextView
        android:id="@+id/no_session_found_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:visibility="gone" />
</RelativeLayout>

<com.gc.materialdesign.views.ButtonFloat
    android:id="@+id/sessions_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:background="@color/primary"
    materialdesign:iconDrawable="@drawable/ic_action_add"/>
</FrameLayout>

活动代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);


}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.home_screen, menu);
    mMenu = menu;
    mSearchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem item = menu.findItem(R.id.action_search);

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    if (searchView != null) {
        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // new searchPhotoSession().execute();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
    }
return true;
}

toolbar.xml是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

应用主题为:

值:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:fitsSystemWindows">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
    </style>

</resources>

值-19:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="windowActionBar">false</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:clipToPadding">false</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:fitsSystemWindows">true</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
</style>

0 个答案:

没有答案