有人可以解释这个异常何时发生?
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类
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);
}
}
};
}
}
答案 0 :(得分:10)
public class IllegalArgumentException extends RuntimeException
抛出表示某个方法已被传递为非法或 不恰当的论点。
在代码中的某处,您将非法参数传递给方法startSettlingAnimation()
(BottomSheetBehavior
类)。这种方法抛出异常:
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);
}
}
您的错误是:Illegal state argument: 5
。 5是STATE_HIDDEN
的int值。因此,当您的状态为STATE_HIDDEN
(5)时,您的mHideable
布尔值为false。因此,基本建议是设置mHideable = true ;
没有任何代码,这就像我告诉你的那样。
答案 1 :(得分:8)
对我来说,问题已由sheetBehavior.setHideable(true);
解决
似乎当setHideable
为假,然后将状态设置为BottomSheetBehavior.STATE_HIDDEN
时会发生
答案 2 :(得分:0)
正如@Serj Ardovic 解释的那样,它是可隐藏的
我想补充一点,你也可以把它放在 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">