Android app crashing trying to use ?attr/variable

  android, textview

I’m trying to use an ?attr/var to set color and it keeps crashing saying the list-view xml has an error. If I use a normal color attribute it works fine.

2021-02-04 18:10:45.621 13812-13812/com.daford.sermonviewer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.viewer, PID: 13812
    android.view.InflateException: Binary XML file line #3 in com.viewer:layout/list_item: Binary XML file line #3 in com.viewer:layout/list_item: Error inflating class android.widget.TextView
    Caused by: android.view.InflateException: Binary XML file line #3 in com.viewer:layout/list_item: Error inflating class android.widget.TextView

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textColor" format="color"/>
</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="LightTheme" parent="AppTheme">
        <item name="textColor">#000000</item>
    </style>

    <style name="DarkTheme" parent="AppTheme">
        <item name="textColor">#ffffff</item>
    </style>

</resources>

list-item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="?attr/textColor"/>

MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTheme(R.style.DarkTheme);

        setContentView(R.layout.activity_main);

  ...

Source: Android Questions

LEAVE A COMMENT