android版本5.1中不支持工具栏

时间:2016-05-16 06:40:19

标签: android android-toolbar

我的应用程序在所有设备上运行,但是当它在Android版本5.1上运行时,它会崩溃并出现以下错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dp.needdepartmentalstore/com.dp.needdepartmentalstore.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.dp.needdepartmentalstore.MainActivity.onCreate(MainActivity.java:69)

下面是style.xml文件

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textSize">16sp</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <style name="WhiteEditText"  parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:textColorHint">@android:color/white</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@android:color/black</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorAccent">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
    </style>
</resources>

下面是style.xml(v21)文件

<resources>>
    <style name="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:textSize">16sp</item>
    </style>
</resources>

下面是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/main_bar"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
            <ImageView
                android:id="@+id/needslogo"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:layout_gravity="start"
                android:contentDescription="@string/logo_content"
                android:src="@drawable/needs_new_logod"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/main_bar"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include layout="@layout/app_bar_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <include layout="@layout/nav_header_main"/>
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/exp_recyclerView"/>
        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
下面的

是我的AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dp.needdepartmentalstore" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.dp.needdepartmentalstore.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="com.dp.needdepartmentalstore.permission.C2D_MESSAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name=".MyApplication"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".CartActivity"
            android:label="My Cart"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name="com.paytm.pgsdk.PaytmPGActivity"
            android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|keyboard"/>
        <activity android:name=".LoginActivity"
            android:theme="@style/WhiteEditText"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:label="Login"/>
        <activity android:name=".ForgotActivity"
            android:theme="@style/WhiteEditText"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:label="Forgot"/>
        <activity android:name=".SignUpActivity"
            android:theme="@style/WhiteEditText"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:label="SignUp"/>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />
        <activity android:name=".ShippingActivity"
            android:label="Proceed Payment"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:parentActivityName=".CartActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".CartActivity" />
        </activity>
        <activity android:name=".NewAddressActivity"
            android:label="Add Address"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:parentActivityName=".ShippingActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ShippingActivity" />
        </activity>
        <activity android:name=".PaymentActivity"
            android:label="Select Payment"
            android:screenOrientation="portrait"
            android:parentActivityName=".ShippingActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ShippingActivity" />
        </activity>
        <activity android:name=".WebActivity"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".NotificationActivity"
            android:label="Notifications"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".OrderHistory"
            android:label="Order History"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".VoucherDetails"
            android:label="Choose Voucher"
            android:screenOrientation="portrait"
            android:parentActivityName=".ShippingActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ShippingActivity" />
        </activity>
        <activity android:name=".OrderSuccessActivity"
            android:label="Thank You"
            android:screenOrientation="portrait"
            android:parentActivityName=".PaymentActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".PaymentActivity" />
        </activity>
        <activity android:name=".OrderFailedActivity"
            android:label="Payment Failed"
            android:screenOrientation="portrait"
            android:parentActivityName=".WebActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".WebActivity" />
        </activity>
        <activity android:name=".MyPersonalInfo"
            android:label="Edit Info"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".Support"
            android:label="Contact Us"
            android:screenOrientation="portrait"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".PaytmTransactionActivity"
            android:label="Proceed Payment"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:parentActivityName=".ShippingActivity">

        </activity>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.dp.needdepartmentalstore" />
            </intent-filter>
        </receiver>
        <service
            android:name=".MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <service
            android:name=".RegistrationIntentService"
            android:exported="false">
        </service>
        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="672994d5600957dc401b99fceee1f6b3ceb0f156" />
    </application>
</manifest>

我该如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

更改..

<resources>>
    <style name="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:textSize">16sp</item>
    </style>
</resources>

要,

<resources>>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:textSize">16sp</item>
    </style>
</resources>

再试一次。

答案 1 :(得分:0)

将以下两行添加到style.xml(v21)

 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

 <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

并改变

 <style name="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:textSize">16sp</item>
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:textSize">16sp</item>
</style>