I'm trying to add android.support.v7.widget.SwitchCompat
programmatically and I'm getting this error:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at android.text.StaticLayout.<init>(StaticLayout.java:422)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:602)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.support.v7.widget.Toolbar.measureChildCollapseMargins(Toolbar.java:1218)
at android.support.v7.widget.Toolbar.onMeasure(Toolbar.java:1326)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:199)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.AppBarLayout$Behavior.onMeasureChild(AppBarLayout.java:888)
at android.support.design.widget.AppBarLayout$Behavior.onMeasureChild(AppBarLayout.java:652)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Chore
but when I'm using Switch
everything is ok. why is this happening? this is my code:
SwitchCompat switchBox = (SwitchCompat) LayoutInflater.from(getApplicationContext()).inflate(R.layout.checkbox, null);
switchBox.setId(i);
Typeface typeface = Typeface.createFromAsset(getAssets(), "iransansfont.ttf");
switchBox.setText(convert_num((nodesModelArrayList.get(i).type ? "بیمارستان " : "مطب ") + nodesModelArrayList.get(i).node_name + ": " + nodesModelArrayList.get(i).stat));
switchBox.setTypeface(typeface);
switchBox.setOnCheckedChangeListener(onCheckedChangeListener);
linearLayout.addView(switchBox);
switchBox.setChecked(true);
Use an activity context and not application context for inflating views so that e.g. themes work.
The crash is because the textOn
and textOff
labels are null that would normally get their default values from a theme.
I had the same issue trying to create the SwitchCompat like this:
SwitchCompat switchCompat = new SwitchCompat(getContext());
switchCompat.setText(...);
Fixed it by inflating it as an XML layout:
layoutInflater.inflate(R.layout.view_switch_compat, parent, false);
And view_switch_compat.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchCompat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Hope it helps someone :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With