0

我的应用程序在较低的 API 上运行良好,因为它们使用 AppCompatTheme,但是当较高的 API 运行应用程序时,会发生下面的异常。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.danfoa.taximetro/com.example.danfoa.taximetro.UnitsActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        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:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
 Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        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:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.example.danfoa.taximetro.UnitsActivity.onCreate(UnitsActivity.java:60)
        at android.app.Activity.performCreate(Activity.java:6010)....

CoordinatedLayout 以及设计库的所有新功能也会发生这种情况。

我不知道这里有什么问题是我的 Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.danfoa.taximetro" >

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission      android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<!-- Requires Open GL 2.0, no permitira que dispositivos sin esta version puedan instalar la aplicacion -->
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat" >
    <!-- android:theme="@style/AppTheme" (I commet it to be able to test the features in lower API's)-->

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyCM6GTw73hMzXMT6bS7oaT2H_esputR65c" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".DataActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".UnitsActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name=".GiftsActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:label="@string/title_activity_gifts" >
    </activity>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    <activity
        android:name=".LogInActivity"
        android:label="@string/title_activity_log_in"
        android:windowSoftInputMode="adjustResize|stateHidden">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.danfoa.taximetro.DataActivity" />
    </activity>
</application>

以及使用设计库的活动的 xml。

DataActivity(扩展 FragmentActivity):

<android.support.v4.widget.DrawerLayout 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"
xmlns:android.support.design="http://schemas.android.com/tools"
android:fitsSystemWindows="true">

<!--MyContent ( Normal Stuff ) -->

<android.support.design.widget.NavigationView
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:id="@+id/navigationView"
    android:layout_gravity="start"
    app:itemIconTint="@color/navigation_view_color_set"
    app:itemTextColor="@color/navigation_view_color_set"
    app:headerLayout="@layout/navigation_view_header"
    app:menu="@menu/menu_data"/>

UnitsActivity(扩展 AppCompatActivity):

<android.support.design.widget.CoordinatorLayout 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"
xmlns:android.support.design="http://schemas.android.com/tools"
android.support.design:context=".UnitsActivity">



<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/units_overcharge_price_layout"/>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout"
    android:layout_height="200dp"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsingToolBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:collapsedTitleTextAppearance="@style/CollapsedTitleAppearance"
        app:expandedTitleTextAppearance="@style/ExpandedTitleAppearance"
        app:contentScrim="@color/primary_dark" >

        <include layout="@layout/taxi_rate_layout"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.1"
            app:layout_scrollFlags="enterAlways"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
             />


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" >


            </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton android:id="@+id/rateFab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    app:fabSize="normal"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_star"
    app:layout_behavior="com.example.danfoa.taximetro.ScrollAwareFABBehavior"
    app:layout_anchor="@id/collapsingToolBarLayout"
    app:layout_anchorGravity="right|bottom|end"
    app:borderWidth="0dp"
    />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp">

    <TextView
        android:id="@+id/price_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/unit_text_background"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="50sp" />

</FrameLayout>

还有 Styles.xml

<resources><!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

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

和样式(v-21).xml

<resources>

<!-- Base application theme for API 21 or higher. -->
<style name="AppTheme" parent="android:Theme.Material">
    <!--Colors-->
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
</style>

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

1 回答 1

1

If you are using AppCompat, your Activity must inherit from an AppCompat theme. In your example, in API 21+ you inherit from a system theme android:Theme.Material which is incorrect.

If you inherit from an AppCompat theme, it will automatically apply the native Material theme under the hood for API 21+ and use AppCompat's Toolbar instead of the native one which may cause conflicts with AppCompat.

于 2015-07-14T23:38:35.343 回答