Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SnackBar causing crash with ScrollView with Illegal State Exception

I am developing an app in which I am having a layout which is wrapped with ScrollView. In fragment of this layout I am hitting a web service to fetch data from server and show it in my layout. Whenever there is an error or no data is available in web service, I am showing a snackbar to the user with the appropriate message in it.

Problem

I am having an issue that only occurs very less often. When sometimes data is not available and snackbar is shown, my app crashes with illegal state exception showing message

ScrollView can host only one direct child

Here is the crash logs

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)

I am using this combination in many screens so I am only posting layout code for one of the 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>

This is the method where I parse data from 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 method

public void showSnackBar(View view, String message) {
    Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
}
like image 963
Vivek Mishra Avatar asked Jul 22 '16 11:07

Vivek Mishra


2 Answers

The problem is that ScrollViews can only have one directly child, and in your code this child is the outter linear layout. When the snackbar is called to be shown, I suppose the view you're requesting the snackbar to be drawn within is the scrollview itself, so there'll be two direct children (your linear layout AND now the views regarding the snackbar)

like image 103
Marcelo Noguti Avatar answered Nov 07 '22 08:11

Marcelo Noguti


As suggested by Marcelo Noguti putting the Scrollview inside a Coordinator Layout and passing it as a view in snackbar method solved the issue for me.

like image 20
Vivek Mishra Avatar answered Nov 07 '22 10:11

Vivek Mishra