android.view.InflateException 使用 selectableItemBackground 时

标签 android android-layout android-view android-attributes

当我的布局膨胀时,我得到了这个异常:

E AndroidRuntime: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
E AndroidRuntime:        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
E AndroidRuntime:        at com.myapp.view.MyRecyclerAdapter.onCreateViewHolder(MyRecyclerAdapter:80)
E AndroidRuntime:        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
E AndroidRuntime:        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
E AndroidRuntime:        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
E AndroidRuntime:        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)

日志中没有“Caused by”,但是我添加了代码来捕获异常并调用getCause()直到它返回null,这是事件的顺序:

android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
Error: Binary XML file line #11: Error inflating class <unknown>
android.content.res.Resources$NotFoundException: File res/drawable-v11/selectable_list_background.xml from drawable resource ID #0x7f020096
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f0100f9 a=-1}

0x7f020096 是 selectable_list_background(见下文),它引用的 TypedValue 是 ?selectableItemBackground

我将异常范围缩小到使用 ?selectableItemBackground。更具体地说,我使用的是 CardView:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/selectable_list_background"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    >

这是 drawable/selectable_list_background 的相关部分:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?selectableItemBackground" />
</selector>

这个activity使用的是我自己的主题,它的父主题是Theme.AppCompat.Light.NoActionBar

这段代码曾经可以工作,但几个月后我才挖出这段代码,现在它崩溃并出现异常。我的猜测是这与将支持库升级到 23.0.1 并将目标 SDK 升级到 23 有关,但我还没有切换回 22 来验证这一点。

如果我删除引用 ?selectableItemBackground 的一行,一切正常。我还尝试了 ?attr/selectableItemBackground?android:attr/selectableItemBackground,但得到了相同的结果。 (后者让我相信支持库可能不是问题)。

编辑:

我在调试器中看了看,怀疑是android.content.res.Resources里面的这段代码,loadDrawable():

Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException {
    [...]
    dr = loadDrawableForCookie(value, id, null);

注意这个函数带了一个主题,但是在调用loadDrawableForCookie()的时候并没有传入,也就是最终触发第一个异常的方法:

java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f0100f9 a=-1}
    at android.content.res.TypedArray.getDrawable(TypedArray.java:867)
    at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:170)
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:115)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2630)
    at android.content.res.Resources.loadDrawable(Resources.java:2540)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
    at android.view.View.<init>(View.java:4280)

这段代码似乎是 Android 6.0 的新代码——5.0 的代码有很大的不同,但是加载 drawable 时传入了主题。并且主题是必需的 AFAICT - 有问题的属性是 AppCompat 的一部分,因此它需要 Activity 的主题来解决它。

不过,这似乎是一个明显的错误,所以我不相信我在这里是在正确的轨道上。有什么想法吗?

最佳答案

正如@Artjom 所说,问题在于在创建自定义 View 时传递 application context。我也面临同样的问题,我将 getApplicationContext() 而不是 activity 作为 context 传递。将 Activity 作为 context 传递后问题解决了。

希望对其他人有所帮助。

关于android.view.InflateException 使用 selectableItemBackground 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33409305/

相关文章:

java - 中止短信广播和/或从收件箱中删除收到的短信

java - 如何使 ImageButton 随机放置在线性布局中? (安卓)

android - 如何对齐中心大圆周围的 8 个小圆圈,如附图所示?

android - listview recyclerview 问题?

Android Studio Recyclerview 使用 Retrofit

java - 尝试打开 asset/database.db 时出错

android - Recyclerview 展开/折叠多个项目

java - Android 仪表板布局不起作用

android - 当组件的可见性设置为 GONE 时,是否可以与其进行交互?

android - sendUserActionEvent() 为空