ShapeDrawable在低于21的api上不起作用

时间:2018-06-30 09:20:19

标签: android android-shapedrawable

我创建了一个shapeDrawable border_white.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="5dp" />
    <stroke
        android:width="2dp"
        android:color="@color/white" />
    <size
        android:width="165dp"
        android:height="40dp" />

    <solid android:color="@android:color/transparent"/>
</shape>

但是当我将其用作背景时,会引发错误:

<TextView
            android:id="@+id/tvCoinFrom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_big"
            android:layout_marginStart="@dimen/margin"
            android:layout_marginLeft="@dimen/margin"
            android:padding="@dimen/padding_small"
            android:textSize="@dimen/text_size_big"
            android:textStyle="bold"
            android:text="BTC"
            android:background="@android:id/background"
            app:layout_constraintBottom_toTopOf="@id/guideline2"
            app:layout_constraintStart_toStartOf="parent"
            style="?style_tv_text_for_primary_color_background"/>

此错误仅在低于21的API中发生,我也在api 27上进行了测试,效果很好。它说错误在于使用该xml作为背景的textview中:

06-30 03:39:37.295 27803-27803/com.mal.saul.coinmarketcap E/CrashlyticsCore: Failed to execute task.
java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask.get(FutureTask.java:173)
    at com.crashlytics.android.core.CrashlyticsBackgroundWorker.submitAndWait(CrashlyticsBackgroundWorker.java:41)
    at com.crashlytics.android.core.CrashlyticsController.handleUncaughtException(CrashlyticsController.java:311)
    at com.crashlytics.android.core.CrashlyticsController$6.onUncaughtException(CrashlyticsController.java:295)
    at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.uncaughtException(CrashlyticsUncaughtExceptionHandler.java:30)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
    at dalvik.system.NativeStart.main(Native Method)
06-30 03:39:37.390 27803-27803/com.mal.saul.coinmarketcap E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mal.saul.coinmarketcap/com.mal.saul.coinmarketcap.Navigation}: android.view.InflateException: Binary XML file line #46: Error inflating class TextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class TextView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at com.mal.saul.coinmarketcap.converter.ConverterFragment.onCreateView(ConverterFragment.java:25)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
    at android.app.Activity.performStart(Activity.java:5382)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
    at android.app.ActivityThread.access$700(ActivityThread.java:159) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:5419) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f080067 a=-1 r=0x7f080067}
    at android.content.res.Resources.loadDrawable(Resources.java:2946)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    at android.view.View.<init>(View.java:3563)
    at android.widget.TextView.<init>(TextView.java:881)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:76)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:72)
    at 

android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:176)         在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)         在android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)

1 个答案:

答案 0 :(得分:1)

检查是否将shapeDrawable border_white.xml文件保存在drawable-v21目录中,然后将此文件剪切并粘贴到drawable目录中。