android.view.InflateException:二进制XML文件行#29:错误膨胀类android.widget.Button

时间:2016-11-10 16:00:09

标签: java android inflate-exception

我收到很多此类错误

android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.Button

我了解到几种不同的布局资源可能会出现这样的错误 任何人都可以解释为什么会出现这种错误吗? 我能做些什么来解决这个问题?

此错误出现在Samsung Galaxy S3 mini Android版本4.2.2上。在使用Android版本5.1.1和6.0.1的其他设备上,按钮和图像显示正确。

布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android.locator.WelcomeActivity">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonForward"
            android:clickable="true"
            android:onClick="ClickForward"
            android:background="@drawable/button_footer_forward_enabled"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Klicke &quot;>&quot; zur Konfiguration"
            android:id="@+id/textView2"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Willkommen"
            android:id="@+id/textView3"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            style="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:clickable="true"
            android:onClick="ClickCancel"
            android:background="@drawable/button_footer_cancel_enabled"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

堆栈追踪:

11-10 14:13:41.156 13504-13504/com.example.android.locator W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x418c5930)
11-10 14:13:41.195 13504-13504/com.example.android.locator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.Button
                                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
                                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
                                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                                                                                    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
                                                                                    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
                                                                                    at android.view.LayoutInflater.inflate(Native Method)
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
                                                                                    at android.app.Activity.setContentView(Activity.java:1932)
                                                                                    at com.example.android.locator.WelcomeActivity$1.onFinish(WelcomeActivity.java:31)
                                                                                    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:176)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5365)
                                                                                    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:1102)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
                                                                                    at dalvik.system.NativeStart.main(Native Method)
                                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                                    at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
                                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666) 
                                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
                                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
                                                                                    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
                                                                                    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631) 
                                                                                    at android.view.LayoutInflater.inflate(Native Method) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360) 
                                                                                    at android.app.Activity.setContentView(Activity.java:1932) 
                                                                                    at com.example.android.locator.WelcomeActivity$1.onFinish(WelcomeActivity.java:31) 
                                                                                    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                    at android.os.Looper.loop(Looper.java:176) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5365) 
                                                                                    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:1102) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
                                                                                    at dalvik.system.NativeStart.main(Native Method) 
                                                                                 Caused by: android.content.res.Resources$NotFoundException: File res/drawable/button_footer_cancel_enabled.xml from drawable resource ID #0x7f02004c
                                                                                    at android.content.res.Resources.loadDrawable(Resources.java:2842)
                                                                                    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                    at android.content.res.XResources$XTypedArray.getDrawable(XResources.java:965)
                                                                                    at android.view.View.<init>(View.java:3542)
                                                                                    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
                                                                                    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
                                                                                    at android.view.View.<init>(Native Method)
                                                                                    at android.widget.TextView.<init>(TextView.java:843)
                                                                                    at android.widget.Button.<init>(Button.java:130)
                                                                                    at android.widget.Button.<init>(Button.java:126)
                                                                                    at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:593) 
                                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666) 
                                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
                                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
                                                                                    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
                                                                                    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631) 
                                                                                    at android.view.LayoutInflater.inflate(Native Method) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360) 
                                                                                    at android.app.Activity.setContentView(Activity.java:1932) 
                                                                                    at com.example.android.locator.WelcomeActivity$1.onFinish(WelcomeActivity.java:31) 
                                                                                    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                    at android.os.Looper.loop(Looper.java:176) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5365) 
                                                                                    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:1102) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
                                                                                    at dalvik.system.NativeStart.main(Native Method) 
                                                                                 Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
                                                                                    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:917)
                                                                                    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858)
                                                                                    at android.content.res.Resources.loadDrawable(Resources.java:2839)
                                                                                    at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
                                                                                    at android.content.res.XResources$XTypedArray.getDrawable(XResources.java:965) 
                                                                                    at android.view.View.<init>(View.java:3542) 
                                                                                    at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
                                                                                    at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631) 
                                                                                    at android.view.View.<init>(Native Method) 
                                                                                    at android.widget.TextView.<init>(TextView.java:843) 
                                                                                    at android.widget.Button.<init>(Button.java:130) 
                                                                                    at android.widget.Button.<init>(Button.java:126) 
                                                                                    at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:593) 
                                                                                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 

转发按钮:

<vector android:height="24dp" android:viewportHeight="47.0"
    android:viewportWidth="60.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#333" android:pathData="m24.8,35c0.2,0 0.5,-0.1 0.6,-0.2l11.3,-11.2c0.2,-0.2 0.3,-0.4 0.3,-0.7 0,-0.3 -0.1,-0.5 -0.3,-0.7l-11.3,-11c-0.2,-0.1 -0.4,-0.2 -0.6,-0.2 0,0 0,0 -0.1,0 -0.3,0 -0.5,0.1 -0.7,0.3l-0.8,1c-0.3,0.4 -0.3,0.9 0.1,1.3l9.1,9.4 -9.1,9.5c-0.4,0.3 -0.4,0.9 -0.1,1.3l0.9,1c0.2,0.1 0.4,0.2 0.7,0.2z"/>
</vector>

取消按钮:

<vector android:height="24dp" android:viewportHeight="47.0"
    android:viewportWidth="60.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#333" android:pathData="m39.5,28.8l-5.5,-5.5 -0.3,-0.3 5.8,-5.8c1,-1 1,-2.7 0,-3.7 -1,-1 -2.7,-1 -3.7,0l-5.8,5.8 -5.8,-5.8c-1,-1 -2.7,-1 -3.7,0 -1,1 -1,2.7 0,3.7l5.8,5.8 -0.3,0.3 -5.5,5.5c-1,1 -1,2.7 0,3.7 1,1 2.7,1 3.7,0l5.5,-5.5 0.3,-0.3 0.3,0.3 5.5,5.5c1,1 2.7,1 3.7,0 1,-1.1 1,-2.7 0,-3.7z"/>
</vector>

WelcomeActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);
    new CountDownTimer(3000,1000){
        @Override
        public void onTick(long millisUntilFinished){}

        @Override
        public void onFinish(){
            //set the new Content of your activity
            WelcomeActivity.this.setContentView(R.layout.activity_welcome);
        }
    }.start();
    getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FFFFFF"));

}

3 个答案:

答案 0 :(得分:1)

您需要修复

android:background="@drawable/button_footer_forward_enabled"

可能

android:background="@drawable/button_footer_cancel_enabled"

您的错误在堆栈跟踪中,只需要阅读它

Resources$NotFoundException: File res/drawable/button_footer_cancel_enabled.xml

您可以通过将矢量drawable放在右侧文件夹中来修复它

XmlPullParserException: Binary XML file line #1: invalid drawable tag vector

您可以通过弄清楚如何正确使用矢量绘图来解决这个问题

最终,好像你想要一个ImageButton。您可以使用app:srcCompat设置正确的drawable,而不是背景属性

答案 1 :(得分:0)

这个答案已经很晚了,但面对这个问题可能对谁有帮助,

我遇到了这个错误,发现问题是我的图像位于特定的可绘制文件夹(我的是设备兼容性)而一般的可绘制文件夹不包含此图像资源

答案 2 :(得分:0)

检查 drawable 的位置。这是一个常见的错误。 您的 button_footer_forward_enabled 可能位于 drawable-v21 而不是 drawable 文件夹中。 只需将其从任何其他文件夹移动到 drawable 文件夹即可解决您的问题。