Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer exception on switch widget in kitkat version

I have used switch widget in my application. when I run app on lollipop, its working fine with it. but when I run in kitkat version, its going to 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)
like image 942
Sagar Maiyad Avatar asked Jan 26 '16 06:01

Sagar Maiyad


2 Answers

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

like image 135
Radu Ionescu Avatar answered Sep 21 '22 21:09

Radu Ionescu


If not use "android:track" property, And Use android:track="@android:color/transparent" instead of @null

like image 43
Jones.Lee Avatar answered Sep 20 '22 21:09

Jones.Lee