Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding SwitchCompat programmatically error

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);
like image 687
Amir_P Avatar asked Jun 23 '16 12:06

Amir_P


2 Answers

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.

like image 67
laalto Avatar answered Sep 23 '22 01:09

laalto


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 :-)

like image 26
voghDev Avatar answered Sep 25 '22 01:09

voghDev