Android Sdk 17中的DrawableBottom错误

时间:2017-11-16 09:04:21

标签: android

我的活动布局中有一个按钮,如下所示:

<Button
        android:id="@+id/button_before"
        android:layout_width="45dp"
        android:layout_height="25dp"
        android:layout_gravity="center_vertical|start"
        android:background="@color/transparent"
        android:drawableBottom="@drawable/ic_navigate_before"
        android:textColor="@color/gray_2" />

当我将应用程序运行到具有Android SDK&gt; 19的设备时,这是可以的,但是当我将我的应用程序运行到Android SDK&lt; 19(如17)的设备时,我的活动很快停止并发生此错误:

11-16 08:57:41.042 3549-3549/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.app/com.myapp.app.SearchActivity}: android.view.InflateException: Binary XML file line #45: Error inflating class Button
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                 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:793)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class Button
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
                                                 at android.view.LayoutInflater.parseInclude(LayoutInflater.java:830)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                 at com.myapp.app.SearchActivity.onCreate(SearchActivity.java:90)
                                                 at android.app.Activity.performCreate(Activity.java:5104)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                 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:793) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                 at dalvik.system.NativeStart.main(Native Method) 
                                              Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_navigate_before.xml from drawable resource ID #0x7f02007d
                                                 at android.content.res.Resources.loadDrawable(Resources.java:1958)
                                                 at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
                                                 at android.widget.TextView.<init>(TextView.java:769)
                                                 at android.widget.Button.<init>(Button.java:107)
                                                 at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
                                                 at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
                                                 at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
                                                 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:675)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
                                                 at android.view.LayoutInflater.parseInclude(LayoutInflater.java:830) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:736) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                 at com.myapp.app.SearchActivity.onCreate(SearchActivity.java:90) 
                                                 at android.app.Activity.performCreate(Activity.java:5104) 
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                 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:793) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                 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:881)
                                                 at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
                                                 at android.content.res.Resources.loadDrawable(Resources.java:1955)
                                                 at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
                                                 at android.widget.TextView.<init>(TextView.java:769) 
                                                 at android.widget.Button.<init>(Button.java:107) 
                                                 at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66) 
                                                 at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62) 
                                                 at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109) 
                                                 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:675) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
                                                 at android.view.LayoutInflater.parseInclude(LayoutInflater.java:830) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:736) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                 at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                 at com.myapp.app.SearchActivity.onCreate(SearchActivity.java:90) 
                                                 at android.app.Activity.performCreate(Activity.java:5104) 
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                 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:793) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                 at dalvik.system.NativeStart.main(Native Method) 

当我从布局中删除此行时,XML活动在每个设备中运行正常:

  

机器人:drawableBottom =&#34; @可绘制/ ic_navigate_before&#34;

ic_navigate_before 是一个XML文件:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="@color/gray_2"
    android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>
</vector>

另外,我的app min SDK是16。 我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以参考此文档了解发生崩溃的原因。

As it stated that 'You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop.&#39;

据我所知, 我们不能直接设置矢量drawable。 从23.3.0版本开始,矢量drawable只能通过app:srcCompat或setImageResource()

加载

尝试以下解决方案, 创建新文件drawable_navigate_before.xml文件,记下代码。

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/ic_navigate_before"/>
 </layer-list>

现在将此文件设置为可在布局文件中绘制的文件。

     android:drawableBottom="@drawable/drawable_navigate_before"

答案 1 :(得分:0)

从23.3.0版本开始,矢量drawable只能通过app:srcCompat或setImageResource()加载。 see here

你可以让VectorDrawable作为drawableLeft(等)工作,但它取决于支持库版本,它带来了价格。我发现这个问题的最佳方法是,从xml中删除drawable bottom并添加drawable bottom编程。

navigateBefore.setCompoundDrawablesWithIntrinsicBounds( 0, 0, 0, R.drawable. ic_navigate_before);