kitkat 版本中开关小部件的空指针异常

Null pointer exception on switch widget in kitkat version

我在我的应用程序中使用了开关小部件。当我 运行 棒棒糖上的应用程序时,它工作正常。但是当我在 kitkat 版本中 运行 时,它会崩溃。

xml设计:

 <Switch
                android:id="@+id/act_edit_device_swtch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:switchMinWidth="40sp"
                android:thumb="@null"
                android:track="@drawable/switchdb" />

switchdb.xml

<?xml version="1.0" encoding="UTF-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/onswitch" android:state_checked="true"/>
    <item android:drawable="@drawable/offswitch" android:state_checked="false"/>
    <item android:drawable="@drawable/offswitch"></item>

</selector>

以下是我的崩溃报告:

java.lang.NullPointerException
       at android.widget.Switch.jumpDrawablesToCurrentState(Switch.java:825)
       at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.widget.FrameLayout.jumpDrawablesToCurrentState(FrameLayout.java:189)
      at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
       at android.widget.FrameLayout.jumpDrawablesToCurrentState(FrameLayout.java:189)
       at android.view.View.onAttachedToWindow(View.java:12146)
       at android.view.ViewGroup.onAttachedToWindow(ViewGroup.java:3499)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.onAttachedToWindow(PhoneWindow.java:2659)
       at android.view.View.dispatchAttachedToWindow(View.java:12620)
       at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1236)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)

使用android:thumb="@android:color/transparent"代替@null

如果不使用 "android:track" 属性,则使用 android:track="@android:color/transparent" 而不是 @null

或者作为一个选项,查看您的样式并检查 textOn 和 textOff 是否不为空。如果是这样,只需删除它们。

<item name="android:textOn"></item>
<item name="android:textOff"></item>