SnackBar 导致 ScrollView 崩溃并出现非法状态异常

SnackBar causing crash with ScrollView with Illegal State Exception

我正在开发一个应用程序,其中有一个用 ScrollView 包装的布局。在此布局的片段中,我正在使用 Web 服务从服务器获取数据并将其显示在我的布局中。每当 Web 服务中出现错误或没有可用数据时,我都会向用户显示一个带有适当消息的快餐栏。

问题

我遇到了一个很少发生的问题。当有时数据不可用并显示 snackbar 时,我的应用程序崩溃并显示消息非法状态异常

ScrollView can host only one direct child

这是崩溃日志

java.lang.IllegalStateException: ScrollView can host only one direct child
                                             at android.widget.ScrollView.addView(ScrollView.java:253)
                                             at android.support.design.widget.Snackbar.showView(Snackbar.java:475)
                                             at android.support.design.widget.Snackbar.handleMessage(Snackbar.java:162)
                                             at android.os.Handler.dispatchMessage(Handler.java:107)
                                             at android.os.Looper.loop(Looper.java:214)
                                             at android.app.ActivityThread.main(ActivityThread.java:6102)
                                             at java.lang.reflect.Method.invoke(Native Method)
                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

我在很多屏幕上都使用这个组合,所以我只发布其中一个 xml 的布局代码

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp">

        <com.xsinfosol.emergency.progressbar.RopeProgressBar
            android:id="@+id/progress_bar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:ropeDynamicLayout="true"
            app:ropePrimaryColor="@color/red"
            app:ropeSecondaryColor="#20FFFFFF"
            app:ropeSlack="0dp"
            app:ropeStrokeWidth="10dp" />

        <ImageView
            android:id="@+id/iv_default_sos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="40dp"
            android:src="@drawable/on_light" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_health"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/medical"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Health"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="2"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_police"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/police"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Police"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="3"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_fire"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/fire"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Fire"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="4"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_no_4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/contact_person"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Contact Person 1"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="5"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_no_5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/contact_person"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Contact Person 2"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_bg">

            <com.xsinfosol.emergency.utils.CustomTextView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/number_circle"
                android:gravity="center"
                android:text="6"
                android:textColor="@color/red"
                android:textSize="22sp" />

            <com.xsinfosol.emergency.utils.CustomTextView
                android:id="@+id/tv_view_no_6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:drawableRight="@drawable/contact_person"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Contact Person 3"
                android:textColor="@android:color/white" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

这是我从 api

解析数据的方法
public void parseSOSResponse(String response) {
    try {
        JSONObject jsonObject = new JSONObject(response);
        String Message=jsonObject.optString("Message");
        int error_code = jsonObject.getInt("error_code");
        if (error_code == 1) {
            String num_4 = jsonObject.getString("num_4");
            String num_5 = jsonObject.getString("num_5");
            String num_6 = jsonObject.getString("num_6");
            String health_number=jsonObject.getString("health_number");
            String fire_number=jsonObject.getString("fire_number");
            String police_number=jsonObject.getString("police_number");
            Profile_complete=jsonObject.getInt("Profile_complete");
            default_sos = jsonObject.getString("default_sos");
            tv_view_no_4.setText(num_4);
            tv_view_no_5.setText(num_5);
            tv_view_no_6.setText(num_6);
            tv_view_fire.setText("Fire \n"+fire_number);
            tv_view_police.setText("Police \n"+police_number);
            tv_view_health.setText("Health \n"+health_number);
            progress_bar1.setMax(100);
            progress_bar1.animate();
            progress_bar1.animateProgress(Profile_complete);
            prefManager.putString(PrefrenceConstants.KEY_EMERGENCY_TYPE,default_sos);
        }else{
            ((ActivityNavigationDrawer)getActivity()).showSnackBar(iv_default_sos,Message);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

showSnackbar 方法

public void showSnackBar(View view, String message) {
    Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
}

尝试使用 LinearLayout 包裹子元素,同时将宽度和高度设置为包裹内容。正如这部分

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp">

问题是ScrollViews只能有一个直接child,而在你的代码中这个child是outter linear layout。当调用显示 snackbar 时,我假设您请求在其中绘制 snackbar 的视图是 scrollview 本身,因此将有两个直接子级(您的线性布局和现在关于 snackbar 的视图)

正如 Marcelo Noguti 所建议的那样,将 Scrollview 放在 Coordinator Layout 并将其作为视图传递给 snackbar 方法解决了我的问题。