AC-OpenSource
AC-OpenSource

Reputation: 357

Measuring switchCompat throws exception

Here is my code for measuring height of items inside a listview and setting the list view height based on the total height of it's children.

I do not have any problems when using any other view.

public static void setListViewHeightBasedOnChildren(TwoWayView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View listItem = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        listItem = listAdapter.getView(i, listItem, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); // this is where the exception happens
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getItemMargin() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom();
    listView.setLayoutParams(params);
    listView.requestLayout();
}

Here's the stack trace:

java.lang.NullPointerException
       at android.text.StaticLayout.<init>(StaticLayout.java:50)
       at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:606)
       at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
       at android.view.View.measure(View.java:15395)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
       at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
       at android.view.View.measure(View.java:15395)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
       at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
       at android.view.View.measure(View.java:15395)

Upvotes: 0

Views: 1022

Answers (2)

hyb1996
hyb1996

Reputation: 147

In Method onMeasue of SwtichCompat, if showText is true, it will make layout with textOn and textOff:

 @Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mShowText) {
        if (mOnLayout == null) {
            mOnLayout = makeLayout(mTextOn);
        }

        if (mOffLayout == null) {
            mOffLayout = makeLayout(mTextOff);
        }
    }
    ...
 }

Have a look at method makeLayout:

private Layout makeLayout(CharSequence text) {
    final CharSequence transformed = (mSwitchTransformationMethod != null)
            ? mSwitchTransformationMethod.getTransformation(text, this)
            : text;

    return new StaticLayout(transformed, mTextPaint,
            transformed != null ?
                    (int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)) : 0,
            Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}

If the param text is null, transformed will be null too. Then it will be passed to the constructor of StaticLayout, causing NullPointException.

So, you should set the attributes android:textOn and android:textOff for SwitchCompat, or set app:showText="false":

<android.support.v7.widget.SwitchCompat
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textOn="@string/text_on"
  android:textOff="@string/text_off" />

or

<android.support.v7.widget.SwitchCompat
  xmlns:app="http://schemas.android.com/apk/res-auto"   
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:showText="false" />

Upvotes: 2

jinkal
jinkal

Reputation: 1702

SwitchCompat requires you to specify android:textOn and android:textOff

example, you can change like this

<android.support.v7.widget.SwitchCompat
android:id="@+id/on_off_switch"
android:textOn="On"
android:textOff="Off"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>

Upvotes: 0

Related Questions