错误:android.view.InflateException:二进制XML文件行

时间:2017-08-19 23:43:56

标签: android xml android-layout android-xml android-styles

当我尝试在旧的物理设备和模拟器(API 19)上运行我的应用程序时出现此错误。但是,当我在我的主要物理设备(API 23)和API 22及更高版本的模拟器上运行时,我没有错误。

这是整个堆栈跟踪:

08-19 19:19:54.615 2799-2799/com.nicoqueijo.android.baseconverter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.nicoqueijo.android.baseconverter, PID: 2799
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicoqueijo.android.baseconverter/com.nicoqueijo.android.baseconverter.activity.MainActivity}: android.view.InflateException: Binary XML file line #78: Error inflating class SeekBar
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                        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:779)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                                     Caused by: android.view.InflateException: Binary XML file line #78: Error inflating class SeekBar
                                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                        at com.nicoqueijo.android.baseconverter.activity.MainActivity.onCreate(MainActivity.java:145)
                                                                                        at android.app.Activity.performCreate(Activity.java:5231)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                        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:779) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                        at dalvik.system.NativeStart.main(Native Method) 
                                                                                     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/custom_thumb.xml from drawable resource ID #0x7f020055
                                                                                        at android.content.res.Resources.loadDrawable(Resources.java:2101)
                                                                                        at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                        at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:73)
                                                                                        at android.widget.SeekBar.<init>(SeekBar.java:83)
                                                                                        at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:46)
                                                                                        at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:42)
                                                                                        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:139)
                                                                                        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.createViewFromTag(LayoutInflater.java:685)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                                        at com.nicoqueijo.android.baseconverter.activity.MainActivity.onCreate(MainActivity.java:145) 
                                                                                        at android.app.Activity.performCreate(Activity.java:5231) 
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                        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:779) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                        at dalvik.system.NativeStart.main(Native Method) 
                                                                                     Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
                                                                                        at android.content.res.TypedArray.getColor(TypedArray.java:327)
                                                                                        at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:970)
                                                                                        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
                                                                                        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
                                                                                        at android.content.res.Resources.loadDrawable(Resources.java:2097)
                                                                                        at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
                                                                                        at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:73) 
                                                                                        at android.widget.SeekBar.<init>(SeekBar.java:83) 
                                                                                        at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:46) 
                                                                                        at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:42) 
                                                                                        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:139) 
                                                                                        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.createViewFromTag(LayoutInflater.java:685) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                                        at com.nicoqueijo.android.baseconverter.activity.MainActivity.onCreate(MainActivity.java:145) 
                                                                                        at android.app.Activity.performCreate(Activity.java:5231) 
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                        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:779) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                        at dalvik.system.NativeStart.main(Native Method) 

这是由我的主要活动xml布局上的第78行引起的,这是这段代码:

<SeekBar
                    android:id="@+id/seekbar_from_controller"
                    style="@style/SeekBarStyle"/>

这追溯到我的搜索栏样式,如下所示:

<style name="SeekBarStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">5dp</item>
        <item name="android:layout_marginRight">15dp</item>
        <item name="android:max">14</item>
        <item name="android:paddingBottom">5dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:progress">1</item>
        <item name="android:thumb">@drawable/custom_thumb</item>
    </style>

但是这会追溯到我的custom_thumb.xml,这是这段代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <size
        android:width="15dp"
        android:height="15dp"/>
    <solid android:color="?colorPrimary"/>
</shape>

我知道问题是我正在使用&#34;?colorPrimary&#34;因为我试图删除它并使用硬编码颜色,然后当我运行它时会再次导致错误,但是在另一个颜色为&#34;?colorPrimary&#34;

的元素上。

有没有办法在不使用硬编码颜色的情况下解决这个问题?我需要使用&#34;?colorPrimary&#34;因为我可以选择更改应用程序的主题,这会更改主要颜色,重音颜色和主要颜色。

1 个答案:

答案 0 :(得分:0)

使用

<solid android:color="@color/colorPrimary"/>