Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating class android.support.v7.widget.Toolbar. My mistake or bug?

I had the following rendering issue on all my layouts when using SDK 22 to preview them.

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

java.lang.NoSuchFieldError: View_theme

In my case , the problem was styles.xml:

XML with rendering problem:

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
    </style>

</resources>

XML without problem:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar" />
    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
    </style>

</resources>

Notice how I had to add @style/ in the parent reference. That seems to solve my problem (after a rebuild).

Question, is this an error on my side, or a bug? Many tutorials don't put it (Including Official Android Page)

Gradle:

compileSdkVersion 22
buildToolsVersion '22.0.1'
minSdkVersion 15
targetSdkVersion 22
classpath 'com.android.tools.build:gradle:1.1.0'

Final Note: I'm not using Toolbar.

like image 699
gian1200 Avatar asked Apr 25 '15 15:04

gian1200


1 Answers

EDIT :

Reading from another SO question.

If your activity extends AppCompactActivity, your parent theme should be

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar" />

where as, if using ActionBarActivity, theme should be

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar" />

I think the difference is on purpose, and not a bug. Please feel free to correct me if i am wrong as i cannot conferm the same from anywhere yet.

OLD :

To use Toolbar as an action bar, the first thing you need to do is disable the decor provided action bar. The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or the light variant).

Use :

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar" />

From this post.

like image 97
Kushal Sharma Avatar answered Nov 05 '22 21:11

Kushal Sharma