膨胀 class ImageView(不是 SVG)时出错

Error inflating class ImageView (not SVG)

AndroidStudio 3.0.1

我的应用有一个 RecyclerView,其中 LinearLayout 是通过自定义适配器- CardAdapter 膨胀的。在 API>=24 上它工作正常,但在 API<24 上它有一个致命错误:

android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
    at com.example.stavnoy.geronimosample.CardAdapter.onCreateViewHolder(CardAdapter.java:31)

(完整日志在底部)

我使用 .jpg 作为 ImageViewsrc,而不是 SVG

这是我为 inflation - list_item:

设计的布局
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_margin="15dp"
    android:weightSum="8"
    android:gravity="center"
    android:background="@android:color/background_light">

    <ImageView
        style="@style/CardElement"
        android:padding="0dp"
        android:layout_margin="0dp"
        android:layout_weight="5"
        android:src="@drawable/unavailable"
        android:layout_gravity="fill_horizontal"
        android:contentDescription="Photo unavailable"/>
    <TextView
        style="@style/CardElement"
        android:textSize="24sp"
        android:text="Lorem Ipsum"
        android:textColor="@android:color/black"
        android:layout_gravity="center_vertical"/>
    <TextView
        style="@style/CardElement"
        android:layout_weight="1"
        android:textSize="16sp"
        android:text="Ipsum Lorem" />

    <View
        android:tag="line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/black"/>

    <LinearLayout
        style="@style/CardElement"
        android:orientation="horizontal"
        android:gravity="end|center_vertical">
        <TextView
            style="@style/CardButton"
            android:text="@string/details_button_caps"
            android:textColor="@android:color/holo_blue_dark"
            android:onClick="details"/>
        <TextView
            style="@style/CardButton"
            android:text="@string/share_button_caps"
            android:textColor="@android:color/black"
            android:onClick="share"/>

    </LinearLayout>

</LinearLayout>

我的卡适配器:

class CardAdapter extends RecyclerView.Adapter {
private final Context context;
private final JSONArray data;

CardAdapter(final JSONArray allCardsData, final Context context) {
    this.data = allCardsData;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new CardViewHolder((LinearLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false));    //line 33
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    //more code
}

我的自定义样式(包括 layout_widthlayout_height):

    <style name="CardButton">
    <item name="android:textSize">18sp</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">end</item>
    <item name="android:paddingRight">4dp</item>
    <item name="android:paddingLeft">4dp</item>
    <item name="android:textAllCaps">true</item>
</style>

<style name="CardElement">
    <item name="android:layout_weight">1</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:paddingLeft">5dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:layout_width">match_parent</item>
</style>

完整日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                  at com.example.stavnoy.geronimosample.CardAdapter.onCreateViewHolder(CardAdapter.java:31)
                  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                  at android.view.View.layout(View.java:13754)
                  at android.view.ViewGroup.layout(ViewGroup.java:4364)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                  at android.view.Choreographer.doFrame(Choreographer.java:525)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                  at android.os.Handler.handleCallback(Handler.java:615)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                at dalvik.sy
Application terminated.

根据添加的样式,我的猜测是(假设样式不是 -v24)重复的属性可能在 API 24+ 中被接受,但在以前的版本中不被接受:

android:padding="0dp"
android:layout_margin="0dp"
android:layout_weight="5"

我会尝试将它们从样式中删除并将其移动到没有明确包含它们的元素,或者只为 ImageView.

创建一个额外的样式

Xavier Rubio Jansana 关于为 src 使用系统可绘制对象的建议奏效了,这导致了问题:糟糕的资源管理。 我将 .jpg 文件存储在 drawable 目录而不是 mipmap 目录中。它适用于直接使用的布局文件,但不适用于inflate()。我仍然不知道为什么 API>=24 设法处理它。