Android.view.InflateException:二进制 XML 文件第 25 行:膨胀类 ImageButton、Resource NotFoundException、XmlPullPar

Posted

技术标签:

【中文标题】Android.view.InflateException:二进制 XML 文件第 25 行:膨胀类 ImageButton、Resource NotFoundException、XmlPullParserException 时出错【英文标题】:Android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton, Resource NotFoundException, XmlPullParserException 【发布时间】:2017-09-30 19:46:51 【问题描述】:

在 API:19 我面临这个 Inflate 异常,但我正在使用 AppCompatDrawableManager,它支持 API:19 中的矢量可绘制对象

三个例外 1. 膨胀图像按钮错误 2. Resources$NotFoundException 来自可绘制资源的文件 res/drawable/bckg_icon.xml 3. org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector

我已尝试将 ImageButton 可绘制 src 降低四种格式(hdpi、mdpi、xxhdpi 等) 我的实施: xml文件是fragmnet xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layoutPreviewActionBar"
android:layout_
android:layout_>


 <RelativeLayout
    android:id="@+id/previewFragment"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">
     <TextView
         android:layout_ 
         android:layout_
         android:layout_alignParentBottom="true" 
         android:layout_marginBottom="25dp"
         android:layout_marginLeft="10dp"
         android:text="00:00"
         android:id="@+id/timeduration"
         android:textColor="@color/colorPrimary"/>
     <ImageButton
         android:id="@+id/localVideo"
         android:layout_
         android:layout_
         android:layout_centerVertical="true"
         android:layout_marginRight="14.1dp"
         android:layout_toLeftOf="@+id/call"
         android:background="@drawable/bckg_icon"
         android:layout_margin="5dp"
         android:src="@drawable/video_icon" />

     <ImageButton
         android:id="@+id/call"
         android:layout_
         android:layout_
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:background="@drawable/initiate_call_button"
         android:clickable="true"
         android:layout_margin="5dp"
         app:srcCompat="@drawable/start_call" />

     <ImageButton
         android:id="@+id/localAudio"
         android:layout_
         android:layout_
         android:layout_centerVertical="true"
         android:layout_marginLeft="14.1dp"
         android:layout_toRightOf="@+id/call"
         android:background="@drawable/bckg_icon"
         app:srcCompat="@drawable/mic_icon" />
</RelativeLayout>

我的Fragment java类那些drawables实现:

rootView = inflater.inflate(R.layout.preview_actionbar_fragment, container, false);
    mAudioBtn = (ImageButton)rootView.findViewById(R.id.localAudio);
    mVideoBtn = (ImageButton)rootView.findViewById(R.id.localVideo);
    mCallBtn = (ImageButton)rootView.findViewById(R.id.call);
    timdura= (TextView)rootView.findViewById(R.id.timeduration); 
    c=mActivity;
    booking_id=mActivity.bookingId;
    drawableStartCall = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.initiate_call_button);
    drawableEndCall =AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.end_call_button);
    drawableBckBtn = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.bckg_icon);

我的 logCat 详细信息:

   I/com. .MainActivity: OnAttach PreviewControlFragment
   I/com.therightdoctors.dpmdoctor.MainActivity: OnCreate PreviewControlFragment
   D/AndroidRuntime: Shutting down VM
   W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a22cf8)
   W/dalvikvm: threadid=1: uncaught exception occurred

   E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfocom.therightdoctors.dpmdoctor/com.therightdoctors.dpmdoctor.MainActivity: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                  at android.os.Looper.loop(Looper.java:193)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                  at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165)
                                                                                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                  at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                                  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
                                                                                  at android.app.Activity.performStart(Activity.java:5353)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                  at android.os.Looper.loop(Looper.java:193) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                                               Caused by: android.content.res.Resources$NotFoundException: File res/drawable/bckg_icon.xml from drawable resource ID #0x7f02005c
                                                                                  at android.content.res.Resources.loadDrawable(Resources.java:2170)
                                                                                  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                  at android.view.View.<init>(View.java:3579)
                                                                                  at android.widget.ImageView.<init>(ImageView.java:123)
                                                                                  at android.widget.ImageButton.<init>(ImageButton.java:87)
                                                                                  at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60)
                                                                                  at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56)
                                                                                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                                                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                                                  at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                  at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165) 
                                                                                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
                                                                                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
                                                                                  at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
                                                                                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
                                                                                  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
                                                                                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174) 
                                                                                  at android.app.Activity.performStart(Activity.java:5353) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                  at android.os.Looper.loop(Looper.java:193) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                                               Caused by: org.xmlpull.v1.XmlPullParserException: Binar

我已经从 *** 尝试了一些类似的问题不起作用

    缩小图片尺寸,

2.在 XML 中使用 app:srcCompat 代替 android:src 属性

仅在 API 中存在问题:19 低 DPI 设备如何处理这些类型的异常

【问题讨论】:

【参考方案1】:

我知道已经有一段时间了,但我认为您在这里使用VectorDrawable 作为ImageButton 的背景:

<ImageButton
     android:id="@+id/localVideo"
     android:layout_
     android:layout_
     android:layout_centerVertical="true"
     android:layout_marginRight="14.1dp"
     android:layout_toLeftOf="@+id/call"
     android:layout_margin="5dp"
     android:src="@drawable/video_icon"
     android:background="@drawable/bckg_icon />"   <!-- <- here is your problem -->

我发现使用VectorDrawable 作为TextViewEditTextButtonImageButton 等的背景使用android:background="..." 会导致API 19 及之前版本的崩溃,因为android:background 在旧版本上设备正在使用旧的 API,当 android 试图夸大你的 VectorDrawable 时,它无法解析它并崩溃。请注意,当您使用 app:srcCompat 作为 ImageView 的源图像时,您使用的是 Compat 库,没有问题。 要解决此问题,您可以使用以下 3 种方法之一:

    在您的视图后面使用ImageView 并将其app:srcCompat 设置为您的VectorDrawable 使用加载 VectorDrawable using Compat library in runtime and sets the background 的自定义小部件 在 API 21 及更高版本中使用alternative resources 将View 的背景设置为VectorDrawable,并在较低的API 中将其设置为BitmapDrawable(例如png 文件)。

【讨论】:

【参考方案2】:

一旦我遇到同样的问题,您可以尝试以下任何一种方法:

使用 .png 或 .jpeg 代替 xml,并确保将 drawable 复制到 drawable 文件夹(有时它会复制到 drawable(v21) 文件夹,然后这些图像将不适用于低于 21 的 API)。

检查.xml drawables的相同内容,它保存在哪个目录中。(您可以通过将视图从Android更改为Project来查看目录结构

在 xml 文件中使用 srcCompat 而不是 src。您可能需要声明

编译'com.android.support:support-vector-drawable:23.4.0'

【讨论】:

选项 1. 我已将 png 用于 android:srcCompact 和背景 xml 资源,它们是矢量可绘制对象,如何转换为 png 或 jpeg。 Option2 drawable文件夹中的所有xml文件和drawable-hdpi,drawable-mdpi,-xhdpi,-xxhdpi,-xxxhdpi文件夹中的png文件,这意味着可绘制。选项 3 我尝试过同样的错误即将到来

以上是关于Android.view.InflateException:二进制 XML 文件第 25 行:膨胀类 ImageButton、Resource NotFoundException、XmlPullPar的主要内容,如果未能解决你的问题,请参考以下文章