Juan Manuel Amoros
Juan Manuel Amoros

Reputation: 357

Error inflating class android.support.v7.widget.Toolbar in Android Lollipop

I use the android.support library for my project, I have developed my app using android 4.4 and everything works good but my problem comes when I try to run the application in a device with Android Lollipop.

When it loads the Activity with the Toolbar, the application stops and returns this error.

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

I have found the same problem on the internet but I haven't found the solution in the answers.

This is my styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
<!-- Base theme applied no matter what API -->
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/dark_primary</item>
    <item name="colorAccent">@color/accent_color</item>
</style>

Here is my styles.xml (v22)

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/dark_primary</item>
        <item name="colorAccent">@color/accent_color</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

To define the toolbar in .xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mitoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:showIn="@layout/activity_main" />

And in my MainActivty I just include that toolbar:

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

And finally the header of my MainActivity.java

public class MainActivity extends android.support.v7.app.ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main); //Here stops my application
}

Heres the logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fitness.dullmonkey.keepingfit/com.fitness.dullmonkey.keepingfit.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5691)
            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:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
     Caused by: android.view.InflateException: Binary XML file line #2: 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.parseInclude(LayoutInflater.java:892)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
            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:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:6142)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5691)
            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:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
            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:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:6142)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5691)
            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:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
     Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 216
            at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
            at android.view.View.<init>(View.java:4084)
            at android.view.ViewGroup.<init>(ViewGroup.java:524)
            at android.view.ViewGroup.<init>(ViewGroup.java:520)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:195)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
            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:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:6142)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5691)
            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:959)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

Thank you in advance for your help.

Upvotes: 1

Views: 9761

Answers (2)

masoud Cheragee
masoud Cheragee

Reputation: 360

in my case the same error but no symptoms of ?attr in android axml file

it looks like a compatibility issue of Xamarin.Appcombat (I found somewhere in web)

try to deploy the same project into lower Android API

in my case Android simulator API 21 had the error above , but another simulator with Android API 18 had no problem at all

Upvotes: 0

headuck
headuck

Reputation: 2783

The cause of the error is that, in the toolbar xml, the ?attr prefix refers to attributes in support library themes. On the other hand, under v22 of style.xml above, the system theme android:Theme.Material.Light.NoActionBar instead of a support library theme is used. The inflater therefore cannot resolve attributes of the support library such as ?attr/actionBarSize.

Using the support library theme Theme.AppCompat.Light.NoActionBar in style.xml (regardless of version) addresses the issue.

Upvotes: 9

Related Questions