Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 测量switchCompat引发异常_Android_Android Appcompat_Switchcompat - Fatal编程技术网

Android 测量switchCompat引发异常

Android 测量switchCompat引发异常,android,android-appcompat,switchcompat,Android,Android Appcompat,Switchcompat,下面是我的代码,用于测量列表视图中项目的高度,并根据其子项的总高度设置列表视图高度 我在使用任何其他视图时没有任何问题 public static void setListViewHeightBasedOnChildren(TwoWayView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int d

下面是我的代码,用于测量列表视图中项目的高度,并根据其子项的总高度设置列表视图高度

我在使用任何其他视图时没有任何问题

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();
}
publicstaticvoidsetListViewHeightBasedOnChildren(双向视图listView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null){
返回;
}
int desiredWidth=MeasureSpec.makeMeasureSpec(listView.getWidth(),最多测量一次);
int totalHeight=0;
View listItem=null;
对于(int i=0;i
以下是堆栈跟踪:

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)
java.lang.NullPointerException
位于android.text.StaticLayout.(StaticLayout.java:50)
位于android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:606)
位于android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
在android.view.view.measure(view.java:15395)
位于android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
位于android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
位于android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
位于android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
在android.view.view.measure(view.java:15395)
位于android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
位于android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
位于android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
位于android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
在android.view.view.measure(view.java:15395)

SwitchCompat要求您指定android:textOn和android:textOff

例如,你可以这样改变

<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"/>

在SwtichCompat的测量方法中,如果showText为true,它将使用textOn on和textfoff进行布局:

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

        if (mOffLayout == null) {
            mOffLayout = makeLayout(mTextOff);
        }
    }
    ...
 }
查看方法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);
}
如果参数文本为null,则转换后的文本也将为null。然后它将被传递给StaticLayout的构造函数,导致NullPointException

因此,您应该为SwitchCompat设置属性android:textOn和android:textOff,或者设置app:showText=“false”:




目前还没有答案……我不确定这是否有用,但我想知道您使用的主题是什么?我的代码在没有textOn和textOff的情况下工作,它只在我尝试测量时崩溃,无论如何,我现在无法访问代码,我想它已经解决了,所以我无法验证此答案。这是两年前提出的问题。我的代码在没有textOn和textOff的情况下工作,只有在我尝试测量它时才会崩溃,无论如何,我现在无法访问代码,我认为它已经解决了,所以我无法验证这个答案。这是两年前提出的。
<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" />
<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" />