8

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.

Community
  • 1
  • 1
gian1200
  • 3,670
  • 2
  • 30
  • 59
  • see this [link][1] this may help you [1]: http://stackoverflow.com/questions/29005958/android-sdk-22-searchview-rendering-problems/29822819#29822819 – N J Apr 25 '15 at 16:03
  • @Nilesh, in my case I'm able to use SDK 22, but only if I add `@style`. No need to downgrade to SDK 21. – gian1200 Apr 25 '15 at 16:42
  • Don't down grade the SDK only change api 22 to 21 in layout file .i.e preview 22 to 21 – N J Apr 25 '15 at 16:45
  • yeah, I meant that. :). By adding @style, I don't need to do that. – gian1200 Apr 25 '15 at 16:47

2 Answers2

4

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.

Community
  • 1
  • 1
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
3

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" />
    
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Eloi Navarro
  • 1,435
  • 1
  • 14
  • 26