我在我的应用程序中使用了switch小部件。当我在棒棒糖上运行app时,它的工作正常。但是当我在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)
答案 0 :(得分:5)
使用android:thumb="@android:color/transparent"
代替@null
答案 1 :(得分:1)
如果不使用“android:track”属性,并使用android:track =“@ android:color / transparent”而不是@null
答案 2 :(得分:0)
或者作为一个选项,看看你的风格,并检查textOn和textOff是否为空。如果是这样,请删除它们。
<item name="android:textOn"></item>
<item name="android:textOff"></item>