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

时间:2018-01-24 00:37:58

标签: android xml android-drawable android-resources android-vectordrawable

我的应用程序在大多数设备上运行完美。但是,每当我尝试在具有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.access$700(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.access$700(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.access$700(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.access$700(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 ,但它仍然只是出现相同的错误。

提前致谢:)

6 个答案:

答案 0 :(得分:36)

关于矢量图形

Documentation说:

  

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

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

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

答案 1 :(得分:5)

要创建矢量可绘制用途,请执行以下操作:

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

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

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

答案 2 :(得分:3)

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

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

然后,使用以下之一:

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

在 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$1.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$2.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)

...当尝试使用 TabLayoutMediator 引用可绘制对象以与 ViewPager2 标签一起使用时:

        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()

...但上述解决方案均无效。

我最终通过直接引用可绘制对象的资源 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

答案 5 :(得分:-4)

Android 4.4(API级别20)及更低版本不支持矢量可绘制对象。

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

编辑

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