Android: Issue with InflateException (Error inflat

2019-08-14 14:08发布

问题:

I am having an issue receiving an RuntimeException and InflateException stating Binary XML file: Error inflating class android.support.v7.widget.Toolbar. I created a project which attempts to use a navigation drawer. I created an android-support-v7-appcompat library (following the directions through google docs) using Android 5.0. In the app, I am extending a NavigationDrawerActivity. I am not sure what is causing this. The libraries seem to be set up correctly, but the app crashes upon launch.

MainActivity.java

public class MainActivity extends NavigationDrawerActivity {

    private static final String LOGTAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // Layout
        setContentView(R.layout.activity_main);

        super.onCreate(savedInstanceState);

NavigationDrawerActivity.java

public class NavigationDrawerActivity extends ActionBarActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use ToolBar and set it as ActionBar
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"     xmlns:app="http://schemas.android.com/apk/res/com.mobile_iab_ad_framework">

<!--     Added fitSystemWindows to allow NavDrawer overlay ActionBar -->

<!--     To display fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

<!--         Use a Toolbar so the drawer can be displayed over ActionBar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            style="@style/HeaderBar"
            app:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

<!--        Listview to display slider menu -->
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="match_parent" 
            android:layout_gravity="start"
            android:background="@color/list_background"
            android:choiceMode="singleChoice"
            android:divider="@color/list_divider"
            android:dividerHeight="0dp"
            android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>

LogCat Log

12-06 03:06:56.487: E/AndroidRuntime(7105): FATAL EXCEPTION: main
12-06 03:06:56.487: E/AndroidRuntime(7105): Process: com.package, PID: 7105
12-06 03:06:56.487: E/AndroidRuntime(7105): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobile_iab_ad_framework/com.package.device_discovery.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.os.Looper.loop(Looper.java:157)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread.main(ActivityThread.java:5356)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at java.lang.reflect.Method.invoke(Method.java:515)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at dalvik.system.NativeStart.main(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.Toolbar
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.createView(LayoutInflater.java:626)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at com.package.device_discovery.MainActivity.onCreate(MainActivity.java:51)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.Activity.performCreate(Activity.java:5431)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-06 03:06:56.487: E/AndroidRuntime(7105):     ... 11 more
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: java.lang.reflect.InvocationTargetException
12-06 03:06:56.487: E/AndroidRuntime(7105):     at java.lang.reflect.Constructor.constructNative(Native Method)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.view.LayoutInflater.createView(LayoutInflater.java:600)
12-06 03:06:56.487: E/AndroidRuntime(7105):     ... 23 more
12-06 03:06:56.487: E/AndroidRuntime(7105): Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.internal.widget.TintTypedArray.getDrawable(TintTypedArray.java:62)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.widget.Toolbar.<init>(Toolbar.java:249)
12-06 03:06:56.487: E/AndroidRuntime(7105):     at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
12-06 03:06:56.487: E/AndroidRuntime(7105):     ... 26 more

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="windowActionBar">false</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="windowActionBar">false</item>
    </style>

    <!--  Extends Theme.Holo.Light and removes the Action Bar -->
    <style name="ThemeWithoutActionBar" parent="android:Theme.Holo.Light">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

    <!-- ************************************************************** -->
    <!-- Action Bar Style (Requires API 11) -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/action_bar_bg</item>
        <item name="android:height">60dp</item>
        <item name="android:titleTextStyle">@style/MyActionBar.TitleTextStyle</item>
    </style>
    <!-- ************************************************************** -->

    <!-- ************************************************************** -->
    <!-- Action Bar Text Style (Requires API 13) -->
    <style name="MyActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:textSize">20sp</item>
    </style>
    <!-- ************************************************************** -->



    <!-- ************************************************************** -->
    <!-- Toolbar Style -->
    <style name="toolbarStyle" parent="Theme.AppCompat">

    </style>

    <style name="ActionBarThemeOverlay" parent="">
        <!-- Color of Toolbar Font Color -->
        <item name="android:textColorPrimary">@android:color/white</item>

        <!-- Color of Toolbar Action Buttons -->
        <item name="colorControlNormal">@android:color/black</item>
        <item name="colorControlHighlight">@android:color/black</item>
    </style>

    <style name="HeaderBar">
        <!-- Color of Toolbar Background Color -->
        <item name="android:background">@color/action_bar_bg</item>
        <item name="android:minHeight">80dp</item>
    </style>

    <style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
        <!-- Color of Toolbar Popup from Action Overflow -->
        <!-- 
        <item name="android:background">@android:color/holo_green_light</item>
        <item name="android:textColor">@android:color/holo_green_dark</item>
        -->
    </style>
    <!-- ************************************************************** -->

</resources>

回答1:

The solution was to use the latest versions of the android-support-v4, android-support-v13, android-support-v7 libraries.



回答2:

maybe this help some people

in my case i didnt use .NoActionBar Theme. i just remove android prefix from this item.

<item name="windowActionBar">false</item>

also do this with other items. in addition i use android studio and gradle shot for you is

'com.android.support:appcompat-v7:22.2.0'

fortunately the error goes away.