レイアウトを拡張すると、次の例外が発生します。
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 に戻していません。
を参照する1行を削除すると、すべて正常に機能します?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 のコードはかなり異なりますが、ドローアブルをロードするときにテーマが渡されます。そしてテーマは必要です AFAICT - 問題の属性は の一部でAppCompat
あるため、それを解決するにはアクティビティのテーマが必要です。
しかし、それはあからさまなバグのように思えるので、ここで正しい軌道に乗っているとは確信していません。何か案は?