Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwitchCompat throwing error

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'])
}
like image 467
Psypher Avatar asked Oct 24 '14 18:10

Psypher


2 Answers

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" />
like image 89
ianhanniballake Avatar answered Nov 10 '22 03:11

ianhanniballake


Had the same error, ended up finding nothing and also landed here. Colleague suggested one small thing which fixed this Exception for me; so I wanted to leave this here:

The SwitchMaterial or SwitchCompat uses a custom defStyleAttr internally. So the default code where I always override the defStyleAttr with 0 when it is not supplied. This overwrites the Style in the parent component and then the crash occurs.

Changing my Constructor from:

class MySwitch(
  context: Context,
  attrs: AttributeSet? = null,
  defStyleAttr: Int = 0
) : SwitchMaterial(context, attrs, defStyleAttr)

to

class MySwitch(
  context: Context,
  attrs: AttributeSet? = null
) : SwitchMaterial(context, attrs)

solved the Problem for me

like image 1
scholt Avatar answered Nov 10 '22 03:11

scholt