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

时间:2016-07-22 11:30:59

标签: android scrollview illegalstateexception android-snackbar

我正在开发一个应用程序,其中我有一个用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$1.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)

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

    <?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();
}

3 个答案:

答案 0 :(得分:4)

问题是ScrollViews只能有一个直接子项,而在你的代码中,这个子项是外部线性布局。当调用快餐栏时,我认为您要求在其中绘制小吃栏的视图是滚动视图本身,因此将有两个直接的孩子(您的线性布局和现在关于小吃店的视图)

答案 1 :(得分:1)

根据Marcelo Noguti的建议,将Scrollview 置于<{1}}内并将其作为快餐栏方法中的视图传递给我解决了问题。

答案 2 :(得分:0)

尝试使用LinearLayout包装子项,同时设置width和height以包装内容。如本部分所述

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