在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)
(完整记录在底部)
我使用src
作为ImageView
,而不是SVG
这是我的通胀布局 - 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>
我的CardAdapter:
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_width
和layout_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.
答案 0 :(得分:0)
根据添加的样式,我的猜测是(假设样式不是-v24
)重复的属性可以在API 24+中接受,但不能在以前的版本中接受:
android:padding="0dp"
android:layout_margin="0dp"
android:layout_weight="5"
我会尝试从样式中删除它们并将其移动到没有明确显示它们的元素,或者仅为ImageView
创建其他样式。
答案 1 :(得分:0)
Xavier Rubio Jansana建议使用可用于src
的系统,这导致了问题:资源管理不善。
我将.jpg文件存储在drawable
目录而不是mipmap
目录中。它适用于直接使用的布局文件,但不适用于inflate()
。我仍然不知道为什么API&gt; = 24设法处理它。