LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); //this line在我的活动的onCreate()方法中有以下代码,在执行最后一行时,我将得到下面的NPE异常:
由: java.lang.NullPointerException:尝试在android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.java:440)上的空对象引用上调用虚拟方法'java.lang.Object java.lang.ref.WeakReference.get()‘引起的
发布于 2016-03-15 01:34:18
虽然Sanf0rds答案是正确的,但它不允许默认情况下将BottomSheet定义为扩展。该问题是由直到最后一行WeakReference才设置的onLayoutChild引起的。
解决方案是提供我们自己的类来扩展BottomSheetBehavior,但是在覆盖的onLayoutChild中设置状态。代码如下所示。
uk/ac/qub/quibe/misc/ExpandedBottomSheetBehavior.java
package uk.ac.qub.quibe.misc;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by mcp on 15/03/16.
*/
public class ExpandedBottomSheetBehavior<V extends View> extends android.support.design.widget.BottomSheetBehavior<V> {
public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
SavedState dummySavedState = new SavedState(super.onSaveInstanceState(parent, child), STATE_EXPANDED);
super.onRestoreInstanceState(parent, child, dummySavedState);
return super.onLayoutChild(parent, child, layoutDirection);
/*
Unfortunately its not good enough to just call setState(STATE_EXPANDED); after super.onLayoutChild
The reason is that an animation plays after calling setState. This can cause some graphical issues with other layouts
Instead we need to use setInternalState, however this is a private method.
The trick is to utilise onRestoreInstance to call setInternalState immediately and indirectly
*/
}
}在布局文件引用中引用新的自定义行为。
变化
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"至
app:layout_behavior="uk.ac.qub.quibe.misc.ExpandedBottomSheetBehavior"发布于 2018-05-25 06:56:48
public class ExpandedBottomSheetBehavior<V extends View> extends
android.support.design.widget.BottomSheetBehavior<V> {
public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
return super.onLayoutChild(parent, child, layoutDirection);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
try {
return super.onInterceptTouchEvent(parent, child, event);
} catch (NullPointerException ignored) {
return false;
}
}
}发布于 2016-03-26 14:57:48
代码的问题是,您试图直接在setState中调用onCreate方法。这将引发一个nullPointer,因为WeakReference尚未初始化。当协调员布局即将放置其子视图时,它将被初始化。
onLayoutChild(CoordinatorLayout parent,V子,int layoutDirection) 当父CoordinatorLayout与布局给定的子视图有关时调用。
因此,最好的方法是将peek高度设置为0,并在onItemClick侦听器中显示/隐藏。
我在这里回答了这个问题:https://stackoverflow.com/a/36236743/1314796
https://stackoverflow.com/questions/35906125
复制相似问题