0

My Android app is crashing because of the following error:

Fatal Exception: android.view.InflateException Binary XML file line
#100 in com.example.myapp:layout/floating_view: Binary XML file line #100 in com.example.myapp:layout/floating_view: Error inflating class <unknown>

The related section of the xml file floating_view.xml, where line #100 is the textColor:

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="?attr/textColor" />

Here it's obviously about the attribute and styles. However, my attribute and styles are defined correctly. As you can see below in my styles.xml, the attribute textColor has a corresponding value in each theme.

<resources xmlns:tools="http://schemas.android.com/tools">

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="textColor">#20282D</item>
    </style>

    <style name="AppThemeLight" parent="AppTheme">
        <item name="textColor">#20282D</item>
    </style>

    <style name="AppThemeDark" parent="AppTheme">
        <item name="textColor">#FFFFFF</item>
    </style>

</resources>

However as I dig further in the error log, it has traces of some other theme names:

Caused by java.lang.UnsupportedOperationException Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x7f040289 a=-1}, theme={InheritanceMap=[id=0x103013fandroid:style/Theme.DeviceDefault.Light.DarkActionBar, id=0x1030238android:style/Theme.Material.Light.DarkActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme], Themes=[android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}

As you can see above, the log from the affected device mentions some themes like Theme.DeviceDefault.Light.DarkActionBar, android:style/Theme.Material.Light etc.. I initially thought it's because my textColor is not defined in those themes. But I have added only 3 themes, and how can I add my custom attribute inside Android's own themes? Do you think the error is because of that, or what else could this error be about?

Also, retrieving the colors in Java code by providing a default value could be an option. However I have so many colors used in my layouts (only in XML) and moving them all to Java is not really ideal in this case.

Any help is appreciated, thanks!

Faruk Yazici
  • 2,344
  • 18
  • 38
  • Read this first you have to call it like this style="@style/BaseText.Margins" https://stackoverflow.com/questions/11225851/android-set-textcolor-with-textappearance-attribute – Elias Fazel Feb 08 '23 at 17:04
  • Does this answer your question? [android: set textColor with textAppearance attribute](https://stackoverflow.com/questions/11225851/android-set-textcolor-with-textappearance-attribute) – Elias Fazel Feb 08 '23 at 17:05
  • I have this in the application tag of my manifest file; android:theme="@style/AppTheme" – Faruk Yazici Feb 08 '23 at 17:10
  • then you don't need to set each element for each view! system will take care of it. – Elias Fazel Feb 09 '23 at 09:52

0 Answers0