gian1200
gian1200

Reputation: 3854

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.

Upvotes: 8

Views: 7344

Answers (2)

Eloi Navarro
Eloi Navarro

Reputation: 1445

In my case, solution was as simple as

  1. Add @style to the parent theme

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    
  2. remove android.support.v7.widget. from Toolbar

  3. move from app:theme to android:theme

    <Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

Upvotes: 3

Kushal Sharma
Kushal Sharma

Reputation: 5973

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.

Upvotes: 4

Related Questions