Anderson K
Anderson K

Reputation: 5505

BottomSheetBehavior Illegal state argument: 5

Could someone explain about when this exception occurs?

12-18 11:20:07.225 15944-15944/com.test.dev.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dev.debug, PID: 15944
java.lang.IllegalArgumentException: Illegal state argument: 5
    at android.support.design.widget.BottomSheetBehavior.startSettlingAnimation(BottomSheetBehavior.java:631)
    at android.support.design.widget.BottomSheetBehavior$1.run(BottomSheetBehavior.java:550)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Java class

public class BottomSheetController {

    private BottomSheetBehavior<View> bottomSheetBehavior;
    private WeakReference<FrameLayout> bottomSheetContainer;
    private Slide slide;

    public interface Slide {
        void onSlide(@NonNull View bottomSheet, float slideOffset);
    }

    public void bind(FrameLayout bottomSheetContainer) {
        this.bottomSheetContainer = new WeakReference<>(bottomSheetContainer);
        bottomSheetBehavior = BottomSheetBehavior.from(this.bottomSheetContainer.get());
        bottomSheetBehavior.setHideable(true);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        bottomSheetBehavior.setBottomSheetCallback(sheetCallback());
    }

    public void unbind() {
        this.bottomSheetContainer.clear();
    }

    public void setSlide(Slide slide) {
        this.slide = slide;
    }

    public void collapse() {
        bottomSheetBehavior.setHideable(true);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    }

    public void expand() {
        bottomSheetBehavior.setHideable(false);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    public void hide() {
        bottomSheetBehavior.setHideable(true);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    }

    public int getState() {
        return bottomSheetBehavior.getState();
    }

    private BottomSheetBehavior.BottomSheetCallback sheetCallback() {
        return new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                if (newState != BottomSheetBehavior.STATE_HIDDEN) {
                    bottomSheetBehavior.setHideable(false);
                } else {
                    bottomSheetBehavior.setHideable(true);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                if (slide != null) {
                    slide.onSlide(bottomSheet, slideOffset);
                }
            }
        };
    }
}

Upvotes: 25

Views: 6749

Answers (3)

As @Serj Ardovic explain it s the Hideable

I wanted to add that you can also put it in the xml :

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="true"   <- this here
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

Upvotes: 2

Vahid
Vahid

Reputation: 1758

For me problem was resolved by sheetBehavior.setHideable(true); It seems when setHideable is false and then setting state to BottomSheetBehavior.STATE_HIDDEN exception will occurred

Upvotes: 11

Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6961

public class IllegalArgumentException extends RuntimeException

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

Somewhere in your code, you are passing an illegal argument to the method startSettlingAnimation() (BottomSheetBehavior class). This method is throwing the exception:

void startSettlingAnimation(View child, int state) {
    int top;
    if (state == STATE_COLLAPSED) {
      top = mCollapsedOffset;
    } else if (state == STATE_HALF_EXPANDED) {
      top = mHalfExpandedOffset;
    } else if (state == STATE_EXPANDED) {
      top = getExpandedOffset();
    } else if (mHideable && state == STATE_HIDDEN) {
      top = mParentHeight;
    } else {
      throw new IllegalArgumentException("Illegal state argument: " + state);
    }
    if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
      setStateInternal(STATE_SETTLING);
      ViewCompat.postOnAnimation(child, new SettleRunnable(child, state));
    } else {
      setStateInternal(state);
    }
  }

Your error is: Illegal state argument: 5. 5 is the int value of STATE_HIDDEN. So while your state is STATE_HIDDEN (5), your mHideable boolean is false. So, the basic suggestion would be to set the mHideable = true;

Without any code, that's as much as I can tell you.

Upvotes: 29

Related Questions