【发布时间】:2015-10-29 08:32:02
【问题描述】:
当我的布局膨胀时,我得到了这个异常:
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>
此活动使用我自己的主题,其父主题是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 的一部分,因此它需要活动的主题来解决它。
不过,这似乎是一个明显的错误,所以我不相信我在这里是在正确的轨道上。有什么想法吗?
【问题讨论】:
-
AppTheme 使用的父主题是哪个?
-
@FabinPaul 对不起,我应该提到这一点。父主题是
Theme.AppCompat.Light.NoActionBar。 -
您是否尝试过使用父主题Base.V7.Theme.AppCompat.Light
-
试过了(当然要调整
windowActionBar和windowNoTitle),但是在膨胀时有同样的异常,具有相同的堆栈跟踪。 -
“我的猜测是这与将支持库升级到 23.0.1 并将目标 SDK 升级到 23 有关,但我还没有切换回 22 来验证这一点。”你是对的,我遇到了同样的问题,需要再次切换到版本 22。 (stackoverflow.com/questions/37376766/…)
标签: android android-layout android-view android-attributes