Skip to content Skip to sidebar Skip to footer

Exception In Scrollview

In my android application, I'm facing a IllegalStateException. I can't reproduce this exception later. This is the stacktrace Non-fatal Exception: java.lang.IllegalStateException

Solution 1:

I examined the question carefully, and I logically found that it is related to showing Snackbar on a fragment or activity, taken as a reference in another class as global object, when it is invalidated in onStop(), onDestroy() life cycle callback stage respectively:

https://stackoverflow.com/a/52019719/787399

Solution 2:

I got the same issue. and I have reproduced using the below steps.

Step 1: make you Fragment Or Activity Layout with a parent of Scrollview.

Step 2: show Snackbar in onPause(), onStop(), onDestroy() like belove.

@OverridepublicvoidonPause() {
        super.onPause();
        Snackbar.make(button, "onPause", Snackbar.LENGTH_LONG).show();
    }

    @OverridepublicvoidonStop() {
        super.onPause();
        Snackbar.make(button, "onStop", Snackbar.LENGTH_LONG).show();
    }

    @OverridepublicvoidonDestroy() {
        super.onDestroy();

        Snackbar.make(button, "onDestroy", Snackbar.LENGTH_LONG).show();
    }

Now run an app and check in logcat. when you click on back button you will get same error mention in question.

Answer :

make a common Snackbar like below.

@OverridepublicvoidonPause() {
            super.onPause();
            showSnackbar("onPause");
        }

        @OverridepublicvoidonStop() {
            super.onPause();
            showSnackbar("onStop");
        }

        @OverridepublicvoidonDestroy() {
            super.onDestroy();
            showSnackbar("onDestroy");
        }

    privatevoidshowSnackbar(String message) {
            if (isValidContext(getActivity())) {
                Snackbar.make(btnConsume, message, Snackbar.LENGTH_LONG).show();
            } 
        }

    publicstaticbooleanisValidContext(final Context context) {

            if (context == null) {
                returnfalse;
            }
            if (context instanceofActivity) {
                final Activity activity = (Activity) context;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    return !activity.isDestroyed() && !activity.isFinishing();
                } else {
                    return !activity.isFinishing();
                }
            }
            returntrue;

        }

Solution 3:

Inside your ScrollView you have to host a child (e.g. a Linear Layout) which at it's time will host all the UI elements from that scrollview. You can't have, for example, 2 textviews added directly to a ScrollView. You need to have something to hold those UI elements inside the scrollview.

Solution 4:

In ScrollView you can have only one View (in View I mean TextView, Button and cetera, but ViewGroup is child of View too) or ViewGroup. So if you have multiple Views put them in a proper ViewGroup and it will work fine.

Solution 5:

Scrollview can only have one direct child

example: This is valid-->

ScrollView
    LinearLayout
        Other Views
        ....
        ....
    LinearLayout
ScrollView

This is not-->

ScrollView
    LinearLayout
        Other Views
        ....
        ....
    LinearLayout
    LinearLayout
        Other Views
        ....
        ....
    LinearLayout

ScrollView

Post a Comment for "Exception In Scrollview"