Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

textview - Android app crashing trying to use ?attr/variable

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);

  ...
question from:https://stackoverflow.com/questions/66056696/android-app-crashing-trying-to-use-attr-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use attr/textColor inside style

<style name="LightTheme" parent="AppTheme">
    <item name=”android:textColor”>?attr/textColor</item>
</style>

then apply this style to textview.

For more details refer Material Design


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...