二进制 XML 文件第 1 行:无效的可绘制标签向量

Binary XML file line #1: invalid drawable tag vector

我有一个应用 运行 在大多数设备上都能完美运行。但是,每当我尝试在 API < 21.

的设备上 运行 我的应用程序时,我都会收到 致命异常

这是日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aceinteract.sleak/com.aceinteract.sleak.activity.LoginRegisterActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access0(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
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:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
at android.app.ActivityThread.access0(ActivityThread.java:158) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5365) 
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:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_person_accent_24dp.xml from drawable resource ID #0x7f07006f
at android.content.res.Resources.loadDrawable(Resources.java:2842)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:1023)
at android.widget.EditText.<init>(EditText.java:76)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:64)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) 
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:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21) 
at android.app.Activity.performCreate(Activity.java:5326) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
at android.app.ActivityThread.access0(ActivityThread.java:158) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5365) 
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:1102)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
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:917)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858)
at android.content.res.Resources.loadDrawable(Resources.java:2839)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
at android.widget.TextView.<init>(TextView.java:1023) 
at android.widget.EditText.<init>(EditText.java:76) 
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:64) 
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60) 
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112) 
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1016) 
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1073) 
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:846) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) 
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:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.aceinteract.sleak.activity.LoginRegisterActivity.onCreate(LoginRegisterActivity.kt:21) 
at android.app.Activity.performCreate(Activity.java:5326) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
at android.app.ActivityThread.access0(ActivityThread.java:158) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5365) 
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:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method) 

这是布局XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layout_register"
    android:visibility="gone"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/edit_register_full_name"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:drawableEnd="@drawable/ic_person_accent_24dp"
        android:padding="20dp"
        android:layout_width="0dp"
        android:hint="@string/hint_full_name"
        android:singleLine="true"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:drawableRight="@drawable/ic_person_accent_24dp" />

    <EditText
        android:id="@+id/edit_register_email"
        app:layout_constraintTop_toBottomOf="@id/edit_register_full_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:drawableEnd="@drawable/ic_email_accent_24dp"
        android:padding="20dp"
        android:layout_width="0dp"
        android:hint="@string/hint_email"
        android:inputType="textEmailAddress"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_email_accent_24dp" />

    <EditText
        android:id="@+id/edit_register_password"
        app:layout_constraintTop_toBottomOf="@id/edit_register_email"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:drawableEnd="@drawable/ic_vpn_key_accent_24dp"
        android:padding="20dp"
        android:inputType="textPassword"
        android:layout_width="0dp"
        android:hint="@string/hint_password"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_vpn_key_accent_24dp" />

    <CheckBox
        android:id="@+id/check_register_show_password"
        android:text="@string/desc_show_password"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/edit_register_password"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

gradle文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.aceinteract.sleak"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '27.0.2'
}

androidExtensions {
    experimental = true
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support:customtabs:27.0.2'
    implementation 'com.android.support:support-vector-drawable:27.0.2'
    implementation 'com.android.support:support-v4:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:animated-vector-drawable:27.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:cardview-v7:27.0.2'
    implementation 'com.android.support:gridlayout-v7:27.0.2'
    implementation 'com.android.support:recyclerview-v7:27.0.2'
    implementation 'com.android.support:design:27.0.2'
    implementation 'com.android.support:palette-v7:27.0.2'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.mikhaellopez:circularimageview:3.0.2'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'uk.co.chrisjenx:calligraphy:2.2.0'
}

我已经尝试更改为 AppComaptEditText,但它仍然给出相同的错误。

提前致谢:)

Documentation 关于 矢量图形 说:

Android 4.4 (API level 20) and lower doesn't support vector drawables.

使用 支持库 您可以使用属性 app:srcCompat 向后兼容,但不向后移植 android:drawableRight

解决方案是在这些情况下继续使用 .PNG 文件或尝试通过代码进行设置。

Android 4.4(API 20 级)及更低版本不支持矢量绘图。

从您的 app/buld.gradle 中删除第 vectorDrawables.useSupportLibrary = true 行。

编辑

也在 app/buld.gradle 中执行。 implementation 'com.android.support:appcompat-v7:[version sdk]'

要创建矢量可绘制对象,请使用:

vector = VectorDrawableCompat.create(context.getResources(), R.drawable.res, null);

或将AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)添加到应用onCreate()方法

注意: 此解决方案不适用于 android:drawableLetf 或 android:drawableRight 但允许使用 app:srcCompat

implementation 'androidx.appcompat:appcompat:{current_version}'

如果问题是由 drawableLeftdrawableRight 引起的,则将 TextView 替换为 androidx.appcompat.widget.AppCompatTextViewEditTextandroidx.appcompat.widget.AppCompatEditText.

然后,使用其中之一:

app:drawableLeftCompat
app:drawableStartCompat
app:drawableEndCompat
app:drawableRightCompat

您可以使用此解决方案获取 5.0 之前的矢量可绘制对象:

Drawable drawable = AppCompatResources.getDrawable(mContext, mImageTitleResId);

当 运行 在 Android 4.4 设备上时,我的项目中有类似的堆栈跟踪,即使它在 Android 7 及更高版本上运行良好:

12-18 19:44:28.453 6660-6660/com.android.gpstest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.gpstest, PID: 6660
    android.content.res.Resources$NotFoundException: File res/drawable/ic_baseline_location_on_24.xml from drawable resource ID #0x7f08009b. If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way. See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info.
        at android.content.res.Resources.loadDrawable(Resources.java:2101)
        at android.content.res.Resources.getDrawable(Resources.java:700)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:457)
        at com.android.gpstest.dialog.ShareDialogFragment$onCreateDialog.onConfigureTab(ShareDialogFragment.kt:62)
        at com.google.android.material.tabs.TabLayoutMediator.populateTabsFromPagerAdapter(TabLayoutMediator.java:170)
        at com.google.android.material.tabs.TabLayoutMediator.attach(TabLayoutMediator.java:138)
        at com.android.gpstest.dialog.ShareDialogFragment.onCreateDialog(ShareDialogFragment.kt:73)
        at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
        at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        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: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
        at android.content.res.Resources.loadDrawable(Resources.java:2097)

...当尝试引用可绘制对象以用于带有 TabLayoutMediatorViewPager2 选项卡时:

        viewPager = view.findViewById(R.id.pager)
        viewPager.offscreenPageLimit = 2
        viewPager.adapter = shareCollectionAdapter
        val tabLayout = view.findViewById(R.id.share_tab_layout) as TabLayout
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            when (position) {
                0 -> {
                    tab.text = getString(R.string.location)
                    tab.icon = ContextCompat.getDrawable(Application.get(),  R.drawable.ic_baseline_location_on_24)
                }
                1 -> {
                    tab.text = getString(R.string.log)
                    tab.icon = ContextCompat.getDrawable(Application.get(),  R.drawable.ic_baseline_list_alt_24)
                }
                2 -> {
                    tab.text = getString(R.string.device)
                    tab.icon = ContextCompat.getDrawable(Application.get(),  R.drawable.ic_baseline_perm_device_information_24)
                }
            }
        }.attach()

...但是 none 上述解决方案有效。

我最终通过直接引用可绘制对象的资源 ID 而不是首先使用 ContextCompat 将它们加载为可绘制对象来解决此问题。

请注意,Kotlin 不会直接解析 tab.icon = R.drawable.x 的直接赋值,因此您需要使用 setter 方法 tab.setIcon(R.drawable.x) 代替。

这是有效的固定代码:

        viewPager = view.findViewById(R.id.pager)
        viewPager.offscreenPageLimit = 2
        viewPager.adapter = shareCollectionAdapter
        val tabLayout = view.findViewById(R.id.share_tab_layout) as TabLayout
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            when (position) {
                0 -> {
                    tab.text = getString(R.string.location)
                    tab.setIcon(R.drawable.ic_baseline_location_on_24)
                }
                1 -> {
                    tab.text = getString(R.string.log)
                    tab.setIcon(R.drawable.ic_baseline_list_alt_24)
                }
                2 -> {
                    tab.text = getString(R.string.device)
                    tab.setIcon(R.drawable.ic_baseline_perm_device_information_24)
                }
            }
        }.attach()

GitHub 上的相关项目问题: https://github.com/barbeau/gpstest/issues/463

提交上述更改以解决 GitHub 上的问题: https://github.com/barbeau/gpstest/commit/1f868b8870a01de2e567a8ca7d094c4341cf567a

只需更改

android:drawableTop - app:drawableTopCompat,

android:drawableBottom - app:drawableBottomCompat,

android:drawableLeft - app:drawableLeftCompat,

android:drawableRight - app:drawableRightCompat.