【已解决】Android中xml中TextView引用已定义好的样式时出错:android.view.InflateException: Binary XML file line #14: Error inflating class

【问题】

android中,在一个xml布局文件中,对于实现已经定义好的样式:

    <style name="NormalText">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">@color/Gray1</item>
    </style>

去引用:

        <TextView
            android:id="@+id/scanDeviceHint"
            android:layout_width="230dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/search_device_hint_text"
            android:textStyle="@style/NormalText">
        </TextView>

结果出错:

04-08 13:40:23.489: E/AndroidRuntime(17541): FATAL EXCEPTION: main

04-08 13:40:23.489: E/AndroidRuntime(17541): java.lang.RuntimeException: Unable to start activity ComponentInfo{yyy/xxx.ScanActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread.access$700(ActivityThread.java:150)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.os.Handler.dispatchMessage(Handler.java:99)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.os.Looper.loop(Looper.java:176)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread.main(ActivityThread.java:5279)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.reflect.Method.invokeNative(Native Method)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.reflect.Method.invoke(Method.java:511)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at dalvik.system.NativeStart.main(Native Method)

04-08 13:40:23.489: E/AndroidRuntime(17541): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.createView(LayoutInflater.java:619)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.Activity.setContentView(Activity.java:1928)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at xxx.ScanActivity.onCreate(ScanActivity.java:46)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.Activity.performCreate(Activity.java:5267)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)

04-08 13:40:23.489: E/AndroidRuntime(17541):     … 11 more

04-08 13:40:23.489: E/AndroidRuntime(17541): Caused by: java.lang.reflect.InvocationTargetException

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.reflect.Constructor.constructNative(Native Method)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.view.LayoutInflater.createView(LayoutInflater.java:593)

04-08 13:40:23.489: E/AndroidRuntime(17541):     … 25 more

04-08 13:40:23.489: E/AndroidRuntime(17541): Caused by: java.lang.NumberFormatException: Invalid int: "@2131165204"

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.Integer.invalidInt(Integer.java:138)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.Integer.parse(Integer.java:375)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at java.lang.Integer.parseInt(Integer.java:366)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.content.res.TypedArray.getInt(TypedArray.java:255)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.widget.TextView.<init>(TextView.java:1171)

04-08 13:40:23.489: E/AndroidRuntime(17541):     at android.widget.TextView.<init>(TextView.java:814)

04-08 13:40:23.489: E/AndroidRuntime(17541):     … 28 more

 

如图:

android textview style reference error android.view.inflateexception binary xml file line infalating class unknown

【解决过程】

1.后来才注意到,其实错误信息中,已经指出来了对应的错误的位置了:

XML file line #14

就是此处上面贴出来的错误的部分。

2.后来去参考:

Styles and Themes | Android Developers

感觉应该是把:

android:textStyle="@style/NormalText"

改为:

style="@style/NormalText"

变为:

        <TextView
            android:id="@+id/scanDeviceHint"
            android:layout_width="230dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/search_device_hint_text"
            style="@style/NormalText">
        </TextView>

结果是:

的确可以了。

 

【总结】

对于Text等内容,去引用已经定义好的Style的话,是:

<TextView style="@style/xxx">

而不是:

<TextView android:style="@style/xxx">

其中,@所引用的xxx的style中, 会包含对应的android:style的相关定义的。



2 Thoughts on “【已解决】Android中xml中TextView引用已定义好的样式时出错:android.view.InflateException: Binary XML file line #14: Error inflating class

  1. 原来是这样!

  2. 学习了,新手总是遇到这些错误,很棒

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量