二进制 XML 文件第 0 行:膨胀类 <unknown> 时出错

Posted

技术标签:

【中文标题】二进制 XML 文件第 0 行:膨胀类 <unknown> 时出错【英文标题】:Binary XML file line #0: Error inflating class <unknown> 【发布时间】:2018-04-23 14:08:22 【问题描述】:

我在运行问答游戏时出错。像这样的东西(当打开游戏活动时)。我的应用程序停止并且我在第 0 行出现二进制 XML 文件错误。这有点奇怪,因为我没有看到第 0 行有这样的问题,所以我需要大家的帮助。谢谢!

E/androidRuntime: FATAL EXCEPTION: main
   Process: sarveshchavan777.triviaquiz, PID: 2901
   java.lang.RuntimeException: Unable to start activity ComponentInfosarveshchavan777.triviaquiz/sarveshchavan777.triviaquiz.MainGameActivity: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6540)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Constructor.newInstance0(Native Method)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
       at android.view.LayoutInflater.createView(LayoutInflater.java:650)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:793)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
       at sarveshchavan777.triviaquiz.MainGameActivity.onCreate(MainGameActivity.java:36)
       at android.app.Activity.performCreate(Activity.java:6980)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
       at android.os.Handler.dispatchMessage(Handler.java:105)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6540)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 1 to color: type=0x5
       at android.content.res.TypedArray.getColor(TypedArray.java:473)
       at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
       at info.hoang8f.widget.FButton.<init>(FButton.java:52)
       at java.lang.reflect.Constructor.newInstance0(Native Method) 
       at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
       at android.view.LayoutInflater.createView(LayoutInflater.java:650) 
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:793) 
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) 
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827) 
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:869) 
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827) 
       at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
       at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
       at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
       at sarveshchavan777.triviaquiz.MainGameActivity.onCreate(MainGameActivity.java:36) 
       at android.app.Activity.performCreate(Activity.java:6980) 
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
       at android.os.Handler.dispatchMessage(Handler.java:105) 
       at android.os.Looper.loop(Looper.java:164) 
       at android.app.ActivityThread.main(ActivityThread.java:6540) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

MainGameActivity.java:36 所在位置:setContentView(R.layout.activity_game_main);

activity_game_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fbutton="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_
    android:layout_
    android:background="@color/backgroundColor"
    android:orientation="vertical"
    tools:context="sarveshchavan777.triviaquiz.MainGameActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:background="@color/lightGreen"
        android:minHeight="?actionBarSize">

        <RelativeLayout
            android:layout_
            android:layout_
            android:padding="0dp">



            <TextView
                android:id="@+id/triviaQuizText"
                android:layout_
                android:layout_
                android:gravity="center"
                android:text="@string/app_name"
                android:textColor="@color/white"
                android:textSize="25sp"
                android:layout_toLeftOf="@+id/timeText"
                android:layout_toRightOf="@+id/coinText" />

            <TextView
                android:id="@+id/timeText"
                android:layout_
                android:layout_
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:paddingLeft="10dp"
                android:gravity="center"
                android:paddingRight="10dp"
                android:text="@string/time"
                android:textSize="20sp" />

        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <View
        android:layout_
        android:layout_></View>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_weight=".5"
        android:background="@color/white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/triviaQuestion"
            android:layout_
            android:layout_
            android:layout_margin="20dp"
            android:gravity="center"
            android:text="@string/question"
            android:textColor="@color/grey"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/resultText"
            android:layout_
            android:layout_
            android:gravity="center"
            android:text=""
            android:textColor="@color/green"
            android:textSize="30sp" />
    </LinearLayout>


    <LinearLayout
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <info.hoang8f.widget.FButton
            android:id="@+id/buttonA"
            android:layout_
            android:layout_
            android:layout_margin="10dp"
            android:onClick="buttonA"
            android:text=""
            android:textColor="@color/grey"
            fbutton:buttonColor="@color/white"
            fbutton:cornerRadius="10dp"
            fbutton:shadowColor="@color/grey" />

        <info.hoang8f.widget.FButton
            android:id="@+id/buttonB"
            android:layout_
            android:layout_
            android:layout_margin="10dp"
            android:onClick="buttonB"
            android:text=""
            android:textColor="@color/grey"
            fbutton:buttonColor="@color/white"
            fbutton:cornerRadius="10dp"
            fbutton:shadowColor="@color/grey" />


        <info.hoang8f.widget.FButton
            android:id="@+id/buttonC"
            android:layout_
            android:layout_
            android:layout_margin="10dp"
            android:onClick="buttonC"
            android:text=""
            android:textColor="@color/grey"
            fbutton:buttonColor="@color/white"
            fbutton:cornerRadius="10dp"
            fbutton:shadowColor="@color/grey" />

        <info.hoang8f.widget.FButton
            android:id="@+id/buttonD"
            android:layout_
            android:layout_
            android:layout_margin="10dp"
            android:onClick="buttonD"
            android:text=""
            android:textColor="@color/grey"
            fbutton:buttonColor="@color/white"
            fbutton:cornerRadius="10dp"
            fbutton:shadowColor="@color/grey" />

    </LinearLayout>


</LinearLayout>

问题出在哪里?

【问题讨论】:

这里:info.hoang8f.widget.FButton.parseAttrs(FButton.java:116) 删除您的应用构建文件夹,制作项目并再次运行。 @pskink 所以这是库错误? @HemantParmar 没用 我不知道 FButton.java 是你的类还是它属于任何库——你更清楚 【参考方案1】:

尝试删除cornerRadius属性导致问题可能是库问题

fbutton:cornerRadius="10dp"

删除所有按钮,效果很好

【讨论】:

【参考方案2】:

问题出在库本身,如果你检查attrs.xml

<attr name="buttonColor" format="color" />

如您所见,buttonColor 只接受颜色值,NOT 颜色引用,并且视图正在尝试将您在 xml 中传递的引用值转换为颜色并引发异常,所以您只能对这个属性使用颜色(这样:#000000)或者fork项目并像这样修改attrs.xml

<attr name="buttonColor" format="color|reference" />

【讨论】:

fbutton:shadowColor 也必须更改 当然!奇怪的是那里没有使用参考

以上是关于二进制 XML 文件第 0 行:膨胀类 <unknown> 时出错的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 13 行:在扩展 VideoView 的类上膨胀类时出错

二进制 XML 文件第 0 行:使用 api 26 膨胀类 TextView 时出错

Xamarin - 二进制 XML 文件第 1 行:膨胀类 <unknown> 时出错

API 19:android.view.InflateException:二进制 XML 文件行 #0:膨胀类 SeekBar 时出错

二进制 XML 文件第 2 行:在 android<21 in android studio 中膨胀类 android.support.v7.widget.CardView 时出错

膨胀类 ImageView 时出错 - android.view.InflateException:二进制 XML 文件第 10 行:膨胀类 ImageView 时出错