Null pointer exception in switch widgets in kitkat version

I used the switch widget in my application. when I run the application on candy, it works great with it. but when i run kitkat version, it will crash.

xml design:

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

below is my crash report:

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)
+4
source share
3 answers

Use android:thumb="@android:color/transparent"instead@null

+4
source

If you do not use the property "android: track", and use android: track = "@android: color / transparent" instead of @null

+1
source

, textOn textOff . , .

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

Source: https://habr.com/ru/post/1625944/


All Articles