40

I am working on a simple app with an activity and i use fragments.One screen with some elements. When i compile and run the app it works fine, except on Samsung Galaxy s3 and note 4. I don't get it from the stacktrace what is wrong.

01-16 16:25:05.915: E/AndroidRuntime(23174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class com.android.internal.widget.ActionBarView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.os.Looper.loop(Looper.java:157)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.main(ActivityThread.java:5335)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Method.invoke(Method.java:515)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at dalvik.system.NativeStart.main(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class com.android.internal.widget.ActionBarView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3253)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3327)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:336)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Activity.setContentView(Activity.java:1973)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.test.MainActivity.onCreate(MainActivity.java:28)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Activity.performCreate(Activity.java:5389)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 11 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.reflect.InvocationTargetException
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 25 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class android.widget.TextView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.initTitle(ActionBarView.java:1131)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.setDisplayOptions(ActionBarView.java:947)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.<init>(ActionBarView.java:392)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 28 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.reflect.InvocationTargetException
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 38 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.content.res.TypedArray.getColor(TypedArray.java:327)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.widget.TextView.<init>(TextView.java:945)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.widget.TextView.<init>(TextView.java:863)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 41 more

And the layout from the fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.locker.theme.one.MainActivity$PlaceholderFragment" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:clickable="true"
        android:gravity="center" >

        <DigitalClock
            android:id="@+id/digitalClock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="66sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:gravity="center" >

        <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyleInverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:visibility="gone" />

        <Button
            android:id="@+id/activateBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/material_deep_teal_200"
            android:gravity="center"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:text="Test" />
    </RelativeLayout>

</LinearLayout>

This is the style i use:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        -->
    </style>

    <!-- Application theme. -->

    <style name="AppTheme" parent="android:Theme.Material">
        <!-- Main theme colors -->
    </style>

</resources>

I did google a bit, but didn't find an idea that could tell me what is happening. Any points would be great. Thank you

3
  • 1
    be sure you import the appcompat library correctly. this was the problem for me. I imported again the library and then add it as reference to the project and clean the project at the end.
    – Gabi Radu
    Commented Feb 1, 2015 at 10:38
  • @PratikButani: Did you have a closer look and the questions and answers to check whether they are different?
    – Lii
    Commented Dec 11, 2019 at 8:52
  • 1
    Yeah @Lii, Its reopened. My mistake was that word color and dimension Commented Dec 11, 2019 at 9:16

4 Answers 4

79

I faced the same issue, that was caused by using attributes as a reference to color in an XML drawable.

As mentioned here on API < 21 you can't use attrs to color in the XML drawable. So only way is to use a reference to a color resource (@color/YOURS_COLOR) or use #RGB format.

So if you want to use an XML drawable with theme depended colors you should create a drawable for each theme.

4
  • Does AppCompat help in this?
    – Qamar
    Commented Jan 18, 2018 at 6:08
  • @Qamar, no. Bug reproduces with AppCompat.
    – mohax
    Commented Jan 18, 2018 at 9:23
  • Also dynamic gradient based on theme attribute colorPrimary and Dark is not possible in API<21 with AppCompat and it's widgets, also they says we replace all views with AppCompat alternative, Google didn't resolve this yet? I created multiple gradient drawables but I think I need to create different layerLists for different themes. Still crash and says Can't convert to color: type=0x2 error
    – Qamar
    Commented Jan 18, 2018 at 9:50
  • Check this out, Loading layer list using ContextCompat.getDrawable(context, R.drawable.welcome_logo_layers); it has <item android:id="@+id/logo_gradient" android:drawable="?attr/logo_gradient" /> and declare in theme <item name="logo_gradient">@drawable/logo_gradient_t1</item>
    – Qamar
    Commented Jan 18, 2018 at 9:53
1

I had the same problem on API 10 devices and it was caused by using theme for API 11+ (from appcompat). For quick solution you may find the following themes declaration in styles.xml

<style name="AppBaseTheme" parent="...">
...
<style name="AppTheme" parent="...">

And change on:

<style name="AppBaseTheme" parent="android:Theme.Light">
...
<style name="AppTheme" parent="android:Theme.Light">
2
  • when I do this I get the following error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    – maysi
    Commented Jul 23, 2015 at 16:28
  • @maysi You can only use an AppCompat theme with an AppCompatActivity, and vice-versa.
    – Sky Kelsey
    Commented Mar 24, 2016 at 6:19
0

Change "AppTheme" to not use Material. For example try:

<style name="AppTheme" parent="android:Theme.Holo">

</style>

I had a very similar issue on my Samsung Galaxy Tab Pro 8.4 Running Android 4.2. It worked when I changed Styles.xml to use "android:Theme.Holo" or "Theme.AppCompat.Light.DarkActionBar".

I haven't been able to find any information on exactly why this happens, and why it seems to only affect Samsung products. If you really want to include Material as a style, you could try adding another style file to use with devices that have API level 21 or higher. More info on that is here under the section "Select a theme based on platform version"

Please keep us updated if you find any other fixes.

0

I ran into this when using 3rd party lib https://github.com/hoang8f/android-flat-button after I upgraded gradle to 3.0.1.

Not ideal, but downgrading gradle seemed to resolve the error

This change requires updating 2 files:

  1. Project build.gradle with

    com.android.tools.build:gradle:2.3.3

  2. gradle/wrapper/gradle-wrapper.properties with

    distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.