5

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)
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99

3 Answers3

5

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

Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
1

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

Jones.Lee
  • 46
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/17225946) – DerApe Sep 04 '17 at 07:37
  • sorry, I encountered the same problem,but Caused by "android:track" property,so I add comment – Jones.Lee Sep 06 '17 at 09:46
0

Or as an option take a look at your style and check if textOn and textOff are not empty. If so just remove them.

<item name="android:textOn"></item>
<item name="android:textOff"></item>
oshurmamadov
  • 264
  • 8
  • 14