19

レイアウトを拡張すると、次の例外が発生します。

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あるため、それを解決するにはアクティビティのテーマが必要です。

しかし、それはあからさまなバグのように思えるので、ここで正しい軌道に乗っているとは確信していません。何か案は?

4

4 に答える 4

7

@Artjomが言うように、問題はapplication contextカスタムビューの作成中に渡すことです。私も同じ問題に直面しており、getApplicationContext()代わりにactivityasを渡していましcontextた。問題が解決したとして通過Activityした後。context

それが他の誰かに役立つことを願っています。

于 2016-09-15T06:00:10.370 に答える
6

android:drawable公式の回答 -または属性にテーマ属性を使用することはできませんandroid:src。設計上、単に不可能です。同じ問題を調査し、解決策を提供する別の回答があります:ドローアブルからスタイル属性を参照する方法は?

私の場合、AppCompat?selectableItemBackgroundがプライベート ドローアブルを参照しているため、実際にはそれができませんでした。そこで、コード ソリューションを選択しました?selectableItemBackground。カードが選択されていない間はフォアグラウンドを設定し、選択モードになったら描画可能な独自の状態リストに設定します。

これは非常に醜いソリューションなので、ここでコードを共有するつもりはありませんが、これまでに思いついた最高のものです。誰かがより良い解決策を持っているなら、私はそれを聞きたいです.

于 2015-11-05T19:25:17.580 に答える