我的项目中存在此错误,仅在api 19(在Android 4.4中)中存在此错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noxef.notecap/com.noxef.notecap.NewEnter}: android.view.InflateException: Binary XML file line #26: Error inflating class android.widget.ToggleButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class android.widget.ToggleButton
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.noxef.notecap.NewEnter.onCreate(NewEnter.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/visibility_password.xml from drawable resource ID #0x7f0700a6
at android.content.res.Resources.loadDrawable(Resources.java:2096)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_visibility.xml from drawable resource ID #0x7f07008e
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
我该怎么办-已经清理过了
我搜索了错误,并在此XML代码中找到了它
就在android:background="@drawable/visibility_password"
中
<ToggleButton
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="0"
android:background="@drawable/visibility_password"
android:gravity="center"
android:textOff=""
android:textOn="" />
我在后台而不是图像中使用Vector XML。
答案 0 :(得分:1)
解决方案:请尝试以下步骤:
首先,将此行添加到ic_visibility.xml
和ic_visibility_off.xml
<vector
...
android:tintMode="multiply"
...>
......
......
</vector>
然后,别忘了添加build.gradle(Module:app)
,否则该方法将不适用于旧版Android。
defaultConfig {
....
vectorDrawables.useSupportLibrary = true
....
}
最后,将其添加到activity
之前的onCreate()
类中:
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
尝试一下。希望能帮助到你。如有任何疑问,请发表评论。
答案 1 :(得分:0)
visibility_password.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_visibility" />
<item android:state_checked="false"
android:drawable="@drawable/ic_visibility_off" />
</selector>
答案 2 :(得分:0)
您的此文件 visibility_password.xml 不得位于 res/drawable 目录中。
将项目文件设置为项目视图。您可能会看到其他一些文件夹,例如“drawable-v24”
将您的 visibility_password.xml 文件移动到 res/drawable 文件夹。
这将解决您的问题