膨胀 class ImageView 时出错 - android.view.InflateException:二进制 XML 文件行 #10:膨胀 class ImageView 时出错
Error inflating class ImageView - android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
我正在设计一个应用程序,其中包含用于列表项 (list_item.xml) 的 xml 文件,
和 listView 的适配器 (NewsAdapter.java).
我的应用在 运行:
时遇到此错误
09-22 20:26:29.595 10387-10387/com.example.rh.newsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rh.newsapp, PID: 10387
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.rh.newsapp.NewsAdapter.getView(NewsAdapter.java:38)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.rh.newsapp.NewsAdapter.getView(NewsAdapter.java:38)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
at android.widget.ImageView.<init>(ImageView.java:160)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.suppor
我的 list_item 文件有一个 ImageView:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:baseline="@id/title"
app:srcCompat="@android:drawable/ic_lock_lock"
tools:ignore="NotSibling" />
当我从 list_item.xml 中删除 ImageView 时,应用程序运行良好!
我该怎么办?!
NewsAdapter.java:
public class NewsAdapter extends ArrayAdapter {
Context context;
List<News> newsList;
public NewsAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List objects) {
super(context, resource, objects);
this.context = context;
this.newsList = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
News current_news = (News) getItem(position);
View list_view = convertView;
if(list_view == null) {
list_view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
TextView title = list_view.findViewById(R.id.title);
TextView publicationDate = list_view.findViewById(R.id.publicationDate);
TextView publication = list_view.findViewById(R.id.publication);
title.setText(current_news.getTitle());
publicationDate.setText(current_news.getPublication_date());
publication.setText(current_news.getPublication());
return list_view;
}
}
list_item.xml:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:baseline="@id/title"
app:srcCompat="@android:drawable/ic_lock_lock"
tools:ignore="NotSibling" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="3"
tools:text="Is technology delivering in schools? Our panel debates" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_alignParentLeft="true"
android:orientation="horizontal">
<TextView
android:id="@+id/publicationDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="27 Nov, 2016" />
<TextView
android:id="@+id/pipe_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/separator_icon" />
<TextView
android:id="@+id/publication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
tools:text="The Guardian" />
</LinearLayout>
</RelativeLayout>
如您在最后几行错误中所见:
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
您似乎输入了一些您必须输入的维度。
并作为属性 android:baseline
:
的文档
The offset of the baseline within this view. May be a dimension value,
which is a floating point number appended with a unit such as
"14.5sp". Available units are: px (pixels), dp (density-independent
pixels), sp (scaled pixels based on preferred font size), in (inches),
and mm (millimeters).
但是您使用了字符串作为基线值。
在 drawable 中复制图像文件时,您可以选择同时复制 drawable 和 drawable-24。确保将两者都复制,然后尝试 运行.
我的应用程序遇到了同样的问题,但最终我解决了它。
只需将您的图像文件放在 ic_launcher_background.xml 文件中而不是 ic_launcher_foreground.xml file.Hope 中,它会有所帮助。
我正在设计一个应用程序,其中包含用于列表项 (list_item.xml) 的 xml 文件, 和 listView 的适配器 (NewsAdapter.java).
我的应用在 运行:
时遇到此错误09-22 20:26:29.595 10387-10387/com.example.rh.newsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rh.newsapp, PID: 10387
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.rh.newsapp.NewsAdapter.getView(NewsAdapter.java:38)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.rh.newsapp.NewsAdapter.getView(NewsAdapter.java:38)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
at android.widget.ImageView.<init>(ImageView.java:160)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.suppor
我的 list_item 文件有一个 ImageView:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:baseline="@id/title"
app:srcCompat="@android:drawable/ic_lock_lock"
tools:ignore="NotSibling" />
当我从 list_item.xml 中删除 ImageView 时,应用程序运行良好!
我该怎么办?!
NewsAdapter.java:
public class NewsAdapter extends ArrayAdapter {
Context context;
List<News> newsList;
public NewsAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List objects) {
super(context, resource, objects);
this.context = context;
this.newsList = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
News current_news = (News) getItem(position);
View list_view = convertView;
if(list_view == null) {
list_view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
TextView title = list_view.findViewById(R.id.title);
TextView publicationDate = list_view.findViewById(R.id.publicationDate);
TextView publication = list_view.findViewById(R.id.publication);
title.setText(current_news.getTitle());
publicationDate.setText(current_news.getPublication_date());
publication.setText(current_news.getPublication());
return list_view;
}
}
list_item.xml:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:baseline="@id/title"
app:srcCompat="@android:drawable/ic_lock_lock"
tools:ignore="NotSibling" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="3"
tools:text="Is technology delivering in schools? Our panel debates" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_alignParentLeft="true"
android:orientation="horizontal">
<TextView
android:id="@+id/publicationDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="27 Nov, 2016" />
<TextView
android:id="@+id/pipe_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/separator_icon" />
<TextView
android:id="@+id/publication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
tools:text="The Guardian" />
</LinearLayout>
</RelativeLayout>
如您在最后几行错误中所见:
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
您似乎输入了一些您必须输入的维度。
并作为属性 android:baseline
:
The offset of the baseline within this view. May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).
但是您使用了字符串作为基线值。
在 drawable 中复制图像文件时,您可以选择同时复制 drawable 和 drawable-24。确保将两者都复制,然后尝试 运行.
我的应用程序遇到了同样的问题,但最终我解决了它。 只需将您的图像文件放在 ic_launcher_background.xml 文件中而不是 ic_launcher_foreground.xml file.Hope 中,它会有所帮助。