Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

Error inflating class android.support.v7.widget.Toolbar?

I am trying to get Material Design for my App by following this method.

Whenever I run the App it shows the following error:

Error inflating class android.support.v7.widget.Toolbar.

I checked whether I have added the appcompat properly. But it is all Correct to me. So please tell me what am I doing wrong ?

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.2'

    defaultConfig {
        applicationId "io.bxbxbai.feedlistviewdemo"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles 'proguard-rules.pro'
        }
    }
}

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':materialDesign')
    compile 'com.balysv.materialmenu:material-menu:1.4.0'
    compile 'com.android.support:appcompat-v7:21.+'
}

Logcat

02:11.641  20583-20583/io.bxbxbai.feedlistviewdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: io.bxbxbai.feedlistviewdemo, PID: 20583
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.bxbxbai.feedlistviewdemo/io.bxbxbai.androiddemos.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class android.support.v7.widget.Toolbar
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5158)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class android.support.v7.widget.Toolbar
            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at io.bxbxbai.androiddemos.MainActivity.onCreate(MainActivity.java:31)
            at android.app.Activity.performCreate(Activity.java:6144)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
????????????at android.app.ActivityThread.access$800(ActivityThread.java:144)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:136)
????????????at android.app.ActivityThread.main(ActivityThread.java:5158)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:515)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
????????????at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
????????????at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
????????????at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
????????????at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
????????????at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
????????????at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
????????????at io.bxbxbai.androiddemos.MainActivity.onCreate(MainActivity.java:31)
????????????at android.app.Activity.performCreate(Activity.java:6144)
????????????at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
????????????at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
????????????at android.app.ActivityThread.access$800(ActivityThread.java:144)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:136)
????????????at android.app.ActivityThread.main(ActivityThread.java:5158)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:515)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
????????????at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable
            at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
            at android.support.v7.internal.widget.TintTypedArray.getDrawable(TintTypedArray.java:62)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:248)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:190)
????????????at java.lang.reflect.Constructor.constructNative(Native Method)
????????????at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
????????????at android.view.LayoutInflater.createView(LayoutInflater.java:594)
????????????at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
????????????at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
????????????at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
????????????at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
????????????at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
????????????at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
????????????at io.bxbxbai.androiddemos.MainActivity.onCreate(MainActivity.java:31)
????????????at android.app.Activity.performCreate(Activity.java:6144)
????????????at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
????????????at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
????????????at android.app.ActivityThread.access$800(ActivityThread.java:144)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:136)
????????????at android.app.ActivityThread.main(ActivityThread.java:5158)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:515)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
????????????at dalvik.system.NativeStart.main(Native Method)

EDIT

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"/>

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_feed_list_demo"
            android:text="@string/feed_list_demo"/>

    </RelativeLayout>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are using new androidx implementations, when typing Toolbar, the Studio will autocomplete it to android.support.v7.widget.Toolbar, but you should use androidx.appcompat.widget.Toolbar instead, otherwise you will get this error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...