25

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);
                }
            }
        };
    }
}
Anderson K
  • 5,445
  • 5
  • 35
  • 50

3 Answers3

28
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.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • Make sense, I've added my controller class. – Anderson K Dec 18 '17 at 15:53
  • 5
    I think the problem is that the `bottomSheetBehavior.setHideable(true);` part is not being called at some conditions inside your `bind()` method, since it also might not be called for some reason. Try debugging your app with break points. – Sergey Emeliyanov Dec 18 '17 at 15:57
  • The test team are running monkey test, it make hard to debug. But i believe that problem is here : `public void onStateChanged(@NonNull View bottomSheet, int newState)` – Anderson K Dec 18 '17 at 16:47
  • I think the accepted answer should be @SerjArdovic 's comment the `bottomSheetBehavior.setHideable(true);` needs to be called. – Michael Osofsky Mar 13 '19 at 18:52
11

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

Vahid
  • 1,588
  • 4
  • 22
  • 34
1

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