85

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>
AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42
  • Also had this issue and realized after a while that I simply forgot to make MyActivity extend ActionBarActivity instead of Activity. Might help someone – MathieuMaree Oct 31 '14 at 15:17
  • 1
    For me the mistake was that i was using instead of – Ajith M A May 23 '15 at 18:06
  • If you upgraded Android Support Library to 23.2.0, try this [answer](http://stackoverflow.com/questions/35739743/file-res-drawable-abc-ic-ab-back-material-xml-from-drawable-resource-id-0x7f020). – Raj Mar 04 '16 at 13:28
  • You're not supposed to change the question after finding an answer – barq Mar 09 '16 at 08:14
  • Updating the support library to the latest version worked for me, go to this [link](https://developer.android.com/topic/libraries/support-library/revisions.html), find the latest version number and update your app gradle file and sync project. – Abduaziz Kayumov Feb 07 '18 at 19:29

24 Answers24

78

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.

JoKr
  • 4,976
  • 8
  • 27
  • 39
37

Sorry Guys. I have solved this Issue long ago. I did a lot of changes. So I can't figure out which one does the trick.

  1. I have changed the id as suggested by Jared Burrows.

  2. Removed my support library and cleaned my project and Re added it.

  3. Go to File -> Invalidate Caches/Restart.

Hope it works.

This is how my code looks now

activity.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">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.arul.anahy.drawer.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

toolbar_default.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"/>

ToolBarStyle

<style name="ToolBarStyle" parent="">
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
 </style>
AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42
32

This happened to me when I tried to use my older code in new android with androidx implementations. change your XML definition to;

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:titleTextColor="@android:color/white" />

then in your java code;

import androidx.appcompat.widget.Toolbar;
Enock Lubowa
  • 679
  • 8
  • 12
24

I was able to solve this problem by replacing the following:

In the Toolbar layout, replace everything which is like this:

android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"

with

android:minHeight="@dimen/abc_action_bar_default_height_material"
android:background="@color/myColor"
mike.b93
  • 1,989
  • 2
  • 18
  • 31
6

I have solved this Issue by this blog:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

The problem is you use the wrong theme,you can choose theme that like Theme.AppCompat.NoActionBar, in my project,it works.

I hope to help you。

Guu
  • 129
  • 2
  • 8
6

I had the same issue as i was using the 23.2.0 version of AppCompat Library. I updated the suppport library to 23.2.1 and the problem was solved.

This Issue of Toolbar has been resolved in 23.2.1

iAviatorJose
  • 418
  • 2
  • 10
  • 25
5

Please read this Google Blog post: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

Also, you are giving it the same "ID" twice, please remove the "id" from your include:

<include android:id="@+id/toolbar" layout="@layout/toolbar" />
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 1
    Why would you remove the namespace? Also, in regards to two references to ids, one id refers to the instance of the toolbar in the layout and the other is the Resource id. So, I think you need both. – William Grand Nov 18 '14 at 15:20
  • @WilliamGrand I am not sure when he "edited" his post. I now see that his toolbar is in a separate file, leaving the namespace is fine. There are two of the same "ids" and his is using the include to bring in that layout into another layout. He does not need the "id" on the include. – Jared Burrows Nov 18 '14 at 17:34
  • 1
    The id on the include will simply override the id used for that element in the layout once it is inflated; that is a feature of include. So long as he uses the correct id in his code it should not hurt anything. – Lorne Laliberte Feb 09 '15 at 19:31
3

I face the same problem but that was after I had updated to appcompatv23.2.0. I tried all the above suggestions but dint work. Then I proceeded to my Gradle file and replaced:

dependencies {
compile 'com.android.support:appcompat-v7:23.2.0’
compile 'com.android.support:design:23.2.0'

}

with:

dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}

Rebuild the project and the issue was gone. It could be an issue with app compact v23.2.0, hopefully a better solution coming up so we can work with the new update.

Hope this helps. Thank you.

nmvictor
  • 1,109
  • 1
  • 15
  • 30
3

I have fixed this problem by modifying app build.gradle file.

For Gradle Plugin 2.0+

 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }  

For Gradle Plugin 1.5

 android {  
   defaultConfig {  

     generatedDensities = []  
  }  

  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 } 
Pacific P. Regmi
  • 1,607
  • 19
  • 15
3

I know this is an old question, but I recently ran into this same issue. It ended up being a Proguard problem (when I set minifyEnabled to false it stopped happening.)

To stop it, with proguard enabled, I added the following to my proguard rules file, thanks to a solution I found elsewhere (after discovering that the problem was proguard)

-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

Not sure if they're necessary, but I also added these lines:

-keep class com.google.** { *; }
-keep interface com.google.** { *; }
Randy
  • 1,068
  • 2
  • 14
  • 32
1

I faced with same problem. Solution that worked for me. If you use v7.Toolbar you must use theme extended from Theme.AppCompat.* You can't use theme extended from android:Theme.Material.* because they have different style attributes.

Hope it will helpful.

Vlad Hudnitsky
  • 1,345
  • 2
  • 11
  • 11
1

To fix this problem . first you must add latestandroid-support-v7-appcompat from the \sdk\extras\android\support

  1. Close the main project.
  2. Remove the android-support-v7-appcompat .
  3. Restart the Eclipse.
  4. Add the android-support-v7-appcompat .
  5. Clean,To build the project.
  6. Then open the main project and build all the projects.
  7. The error still remains. Restart eclipse. That's it.

That works for me and I will strongly recommend you to use Android Studio.

Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
1

I removed these lines as below :

before :

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="@attr/actionBarSize"
    android:minHeight="@attr/actionBarSize"
    android:layout_alignParentTop="true" >

after :

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true" >

Instead of "@attr/actionBarSize" put specific dimens it works for me.

Prashant
  • 66
  • 9
1

This worked for me: Add compile 'com.android.support:appcompat-v7:21.0.3' to the gradle. Change the sdk target to 21. Hope it works for you!

Claudio
  • 11
  • 2
1

For me it worked after I did:

  • Cleaning solution.
  • Deleting the app from the phone I've debugged it at.
  • Closing Visual Studio.
  • Deleting folders /bin/ and /obj/ in android projects.
  • Launching solution again.
nvbnvb
  • 76
  • 5
1

None of the above solutions worked for me.

I didn't have a toolbar in my project, but got the same error.

I cleaned up the project, uninstalled the app. Then I ran a gradlew build --refresh-dependencies, and found out there were some onclick events without corresponding code in the xml files.

I removed them, rebuilt the project, and it worked.

The dependencies didn't seem like were updated, but that's another story.

live-love
  • 48,840
  • 22
  • 240
  • 204
1

In the case of Xamarin in VS, you must add

Theme = "@style/MyThemesss"

to youractivity.cs.

I add this and go on.

sa1
  • 55
  • 8
1

In my case I was getting this error in Inflation exception on Imageview, in the lower versions and lollipop OS.

I resolved this exception when moving all image file in drawable v-24 folder to drawable folder.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

I had issues including toolbar in a RelativeLayout, try with LinearLayout. If you want to overlay the toolbar, try with:

 <RelativeLayout>

    <LinearLayout>
      --INCLUDE tOOLBAR--
    </LinearLayout>

    <Button></Button>

 </RelativeLayout>

I don't understand why but it works for me.

Juanjo
  • 298
  • 1
  • 15
0

It is unclearly why this error happened with me but I solved. I used same layout, in-line or using include, both of them cause NPE error. So I think it's not layout issue.

I had an abstract class call BaseActivity extends ActionBarActivity which have initActionBar() method. I override and call this method in OnCreate of child class. Something like that:

android.support.v7.app.ActionBar mActionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_appointment);

    // Inject View using ButterKnife
    ButterKnife.inject(this);

    // Init toolbar & status bar
    initActionBar();
}

@Override
protected void initActionBar() {
    super.initActionBar();

    setSupportActionBar(mToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setHomeButtonEnabled(true);
}

I HAD NPE ERROR WITH ABOVE CODE. I DON'T KNOW WHY I'M WRONG. I SOLVE BY BELOW CODE AND IT'S LOOK SAME.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_appointment);
    ButterKnife.inject(this);

    setSupportActionBar(mToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setHomeButtonEnabled(true);

    initActionBar();
}

@Override
protected void initActionBar() {
    super.initActionBar();
}
Khai Nguyen
  • 3,065
  • 1
  • 31
  • 24
0

In my case, a problem was in calling inflater.inflate(R.layout.some_layout, null). Null as ViewGroup parameter was the problem.

Oknesif
  • 526
  • 5
  • 11
0

The solution to the problem for me was found in the XML document for my Main Activity. Originally my toolbar was <android.support.v7.widget.Toolbar. To resolve this I changed it to <android.widget.Toolbar. I do not know why this worked though. Does anyone have any insight as to why?

androidtitan
  • 671
  • 2
  • 11
  • 21
0

Problem was for pre-lolipop devices. I was able to solve this problem by replacing the following:

In the Toolbar layout, replace everything which is like this:

android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"

with

android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
0

For me, the error was that I had:

    <android:support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

instead of:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

Specifically, I had a colon between "android" and "support" on the first line instead of a period.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101