Drawable 不适用于 API 19

the*_*ani 2 java xml android android-layout

我在 RecyclerView 中添加了一个 FastScroller 并使用了一个“气泡”-Drawable。

如果我在 API 21 上测试我的应用程序,它可以工作,但如果我在 API 19 上测试它,它会崩溃:

java.lang.RuntimeException: 无法启动活动 ComponentInfo{at.guger.musixs/at.guger.musixs.ui.MainActivity}:

android.view.InflateException:二进制 XML 文件第 15 行:错误膨胀类 at.guger.fastscroll.FastScroller ... 引起:android.view.InflateException:二进制 XML 文件第 15 行:错误膨胀类 at.guger.fastscroll .FastScroller

...引起:java.lang.reflect.InvocationTargetException

... 引起:android.view.InflateException:二进制 XML 文件第 6 行:错误膨胀类 at.guger.fastscroll.FastScrollBubble

...引起:java.lang.reflect.InvocationTargetException

... 引起:android.content.res.Resources$NotFoundException: File res/drawable/bubble.xml from drawable resource ID #0x7f02004b

在 android.content.res.Resources.loadDrawable(Resources.java:3457)

在 android.content.res.TypedArray.getDrawable(TypedArray.java:602)

在 android.view.View.(View.java:3767)

在 android.view.ViewGroup.(ViewGroup.java:481)

在 android.widget.FrameLayout.(FrameLayout.java:101)

在 android.widget.FrameLayout.(FrameLayout.java:97)

在 at.guger.fastscroll.FastScrollBubble.(FastScrollBubble.java:0)

... 38

我的气泡 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:topLeftRadius="@dimen/bubble_corner_radius"
        android:topRightRadius="@dimen/bubble_corner_radius"
        android:bottomLeftRadius="@dimen/bubble_corner_radius"
        android:bottomRightRadius="0dp" />

    <solid android:color="?attr/colorAccent" />

    <size
        android:height="@dimen/bubble_size"
        android:width="@dimen/bubble_size" />
</shape>
Run Code Online (Sandbox Code Playgroud)

我的 FastScroll-Bubble-Layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fastscroll_bubble"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:background="@drawable/bubble"
    android:gravity="center"
    android:textSize="36sp"
    tools:text="A"
    tools:visibility="visible" />
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构:

tyn*_*ynn 6

您的问题是?attr/在 drawable 中的使用

<solid android:color="?attr/colorAccent" />
Run Code Online (Sandbox Code Playgroud)

这仅支持 Lollipop up。因此对于以下所有版本,您需要直接将颜色定义为颜色资源。

<solid android:color="@color/colorAccent" />
Run Code Online (Sandbox Code Playgroud)

请参阅如何从可绘制对象引用样式属性?了解更多详细信息。