SwitchCompat throwing error

Psypher picture Psypher · Oct 24, 2014 · Viewed 8.5k times · Source

I am trying to use switchcompat in my fragment. The min supported API is 14 and max is 21. I am trying to apply the material view to switch for all pre lollipop android versions. But when use the below code I get an error: How can I correct it?

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Inflating in java code which is throwing the exception:

 @Override                                                                                       
 public View onCreateView(LayoutInflater inflater, ViewGroup container,                          
                          Bundle savedInstanceState) {                                           
     final View view = inflater.inflate(R.layout.fragment_scheduler_ui, container, false);   

Reference: http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html

Exception:

10-24 14:15:15.880    1561-1561/com.stackoverflow.ranjith.androidprojdel E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.stackoverflow.ranjith.androidprojdel, PID: 1561
java.lang.NullPointerException
        at android.text.Layout.getDesiredWidth(Layout.java:67)
        at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:570)
        at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:495)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

Build.gradle:

dependencies {
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:palette-v7:+'
    compile 'com.android.support:support-v4:+'
    compile "com.android.support:appcompat-v7:21.0.+"
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Answer

ianhanniballake picture ianhanniballake · Oct 25, 2014

SwitchCompat requires that you set the android:textOn and android:textOff values unless you set app:showText="false":

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="ON"
    android:textOff="OFF" />

or (assuming you have xmlns:app="http://schemas.android.com/apk/res-auto" at the top of your XML file)

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:showText="false" />