Android studio 动画选择器在视图初始化时导致崩溃

Android studio 动画选择器在视图初始化时导致崩溃,android-studio,kotlin,Android Studio,Kotlin,我的目标是创建一个自定义开关,包括选中和未选中状态之间的动画转换 我很容易获得一个默认状态,我的开关在这两种状态下看起来都和预期的一样,当状态改变时,它会改变它的外观 当我附加动画选择器而不是常规选择器时,会出现问题 我遇到了这样一个错误: android.view.InflateException:二进制XML文件行#10:二进制XML 文件行#10:膨胀类android.widget.Switch时出错 原因:android.content.res.Resources$NotFoundExc

我的目标是创建一个自定义开关,包括选中和未选中状态之间的动画转换

我很容易获得一个默认状态,我的开关在这两种状态下看起来都和预期的一样,当状态改变时,它会改变它的外观

当我附加动画选择器而不是常规选择器时,会出现问题

我遇到了这样一个错误:

android.view.InflateException:二进制XML文件行#10:二进制XML 文件行#10:膨胀类android.widget.Switch时出错

原因:android.content.res.Resources$NotFoundException:文件 res/drawable/switch\u track\u animation\u unchecked\u checked.xml from 可提取资源ID#0x7f060072 位于android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)

原因:android.content.res.Resources$NotFoundException:文件 可绘制资源中的res/drawable/switch\u track\u animated\u selector.xml ID#0x7f060070

我可以保证我正确地使用了这些可抽取的东西等

我经历了无效缓存和重新启动,清理构建,重建等,但它不会工作

查看示例

<Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:thumb="@drawable/switch_thumb_selector"
            android:track="@drawable/switch_track_animated_selector"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:checked="true"
            android:id="@+id/switch2"/>

我希望此视图能够加载,并且不会在视图初始化时抛出错误。

您的问题是
切换\u跟踪\u动画\u未选中\u已选中。xml
切换\u跟踪\u动画\u已选中\u未选中。xml
是动画的
矢量可绘制文件
,并且这些动画状态是静态的
矢量可绘制文件
状态。您正在尝试设置
ShapeDrawable
的动画,而不是
VectorDrawable
,我认为这就是导致异常的原因


您需要将
开关打开.xml
开关关闭.xml
替换为
实现,以便在
中为它们设置动画,虽然我不确定它是否真的能够实现您在此处尝试实现的目标。

您的切换轨道动画选择器似乎没有没有任何id的默认状态。您可以尝试添加此状态。这在类似的情况下帮助了我

嘿,谢谢你的回复。我在使用vector drawables时也遇到了同样的问题。我将更新我的问题,以便您可以看到这些向量。但这没用。
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/checked"
          android:drawable="@drawable/switch_bckg_on"
          android:state_checked="true"/>

    <item android:id="@+id/unchecked"
          android:drawable="@drawable/switch_bckg_off"
          android:state_checked="false"/>

    <transition
            android:fromId="@+id/unchecked"
            android:toId="@+id/checked"
            android:drawable="@drawable/switch_track_animation_unchecked_checked"/>

    <transition
            android:fromId="@+id/checked"
            android:toId="@+id/unchecked"
            android:drawable="@drawable/switch_track_animation_checked_unchecked"/>
</animated-selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00B05A"/>
    <stroke android:width="1dp" android:color="#00B05A" />
    <corners android:radius="180dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F1F1F1"/>
    <stroke android:width="1dp" android:color="#E0E0E0" />
    <corners android:radius="180dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:aapt="http://schemas.android.com/aapt"
                 android:drawable="@drawable/switch_bckg_on">
    <target android:name="android:drawable">
        <aapt:attr name="android:animation">
            <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:interpolator="@android:interpolator/accelerate_decelerate"
                    android:propertyName="strokeColor"
                    android:valueFrom="#A0A0A0"
                    android:valueTo="#1E9618"
                    android:valueType="intType" />
        </aapt:attr>
    </target>
</animated-vector>
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:aapt="http://schemas.android.com/aapt"
                 android:drawable="@drawable/switch_bckg_off">
    <target android:name="android:drawable">
        <aapt:attr name="android:animation">
            <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:interpolator="@android:interpolator/accelerate_decelerate"
                    android:propertyName="strokeColor"
                    android:valueFrom="#A0A0A0"
                    android:valueTo="#1E9618"
                    android:valueType="intType"/>
        </aapt:attr>
    </target>
</animated-vector>
<vector android:height="33dp" android:viewportHeight="300"
    android:viewportWidth="300" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:name="background_off" android:fillColor="#FFFFFF" android:fillType="nonZero"
        android:pathData="M149.926,149.926m-149.176,0a149.176,149.176 0,1 1,298.352 0a149.176,149.176 0,1 1,-298.352 0"
        android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
    <path android:name="icon_off" android:fillColor="#9B9B9B" android:fillType="evenOdd"
        android:pathData="M166.88,146.104C166.13,145.354 166.13,144.228 166.88,143.478L195.019,115.339C195.77,114.588 196.145,113.463 196.145,112.712C196.145,111.962 195.77,110.836 195.019,110.086L189.767,104.833C189.016,104.083 187.891,103.708 187.14,103.708C186.015,103.708 185.264,104.083 184.514,104.833L156.375,132.972C155.624,133.723 154.499,133.723 153.748,132.972L125.609,104.833C124.859,104.083 123.733,103.708 122.983,103.708C122.233,103.708 121.107,104.083 120.357,104.833L115.104,110.086C114.354,110.836 113.979,111.962 113.979,112.712C113.979,113.463 114.354,114.588 115.104,115.339L143.243,143.478C143.994,144.228 143.994,145.354 143.243,146.104L115.104,174.243C114.354,174.993 113.979,176.119 113.979,176.869C113.979,177.62 114.354,178.745 115.104,179.496L120.357,184.748C121.107,185.499 122.233,185.874 122.983,185.874C123.733,185.874 124.859,185.499 125.609,184.748L153.748,156.609C154.499,155.859 155.624,155.859 156.375,156.609L184.514,184.748C185.264,185.499 186.39,185.874 187.14,185.874C187.891,185.874 189.016,185.499 189.767,184.748L195.019,179.496C195.77,178.745 196.145,177.62 196.145,176.869C196.145,176.119 195.77,174.993 195.019,174.243L166.88,146.104Z"
        android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
<vector android:height="33dp" android:viewportHeight="302"
    android:viewportWidth="302" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:name="background_on" android:fillColor="#FFFFFF" android:fillType="nonZero"
        android:pathData="M151,151m-150.25,0a150.25,150.25 0,1 1,300.5 0a150.25,150.25 0,1 1,-300.5 0"
        android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
    <path android:name="icon_on" android:fillColor="#00B05A" android:fillType="evenOdd"
        android:pathData="M105.523,153.198C104.806,152.5 104.448,151.453 104.448,150.754C104.448,150.056 104.806,149.009 105.523,148.31L110.536,143.422C111.968,142.026 114.117,142.026 115.549,143.422L115.907,143.772L135.602,164.371C136.318,165.069 137.393,165.069 138.109,164.371L186.093,115.841L186.451,115.841C187.883,114.444 190.032,114.444 191.464,115.841L196.477,120.728C197.91,122.125 197.91,124.22 196.477,125.616L139.183,183.573C138.467,184.272 137.751,184.621 136.676,184.621C135.602,184.621 134.886,184.272 134.17,183.573L106.239,154.246L105.523,153.198Z"
        android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "...customswitch"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}