NullPointerException与AppCompat BottomSheets [英] NullPointerException with AppCompat BottomSheets
问题描述
LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); //this line
我在活动的onCreate()
方法中包含此代码,并且在执行最后一行时出现以下NPE异常:
I have this code within my activity's onCreate()
method and I'm getting the below NPE exception when the last line is executed:
由以下原因引起:java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'java.lang.Object java.lang.ref.WeakReference.get()' 在android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.java:440)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference at android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.java:440)
推荐答案
虽然Sanf0rds答案正确,但是它不允许将BottomSheet定义为默认展开的功能.造成此问题的原因是,直到onLayoutChild的最后一行才设置WeakReference.
While Sanf0rds answer is correct, it doesn't allow the ability to define the BottomSheet as expanded by default. The issue is caused by the WeakReference not being set until the last line of onLayoutChild.
解决方案是提供我们自己的类,该类扩展BottomSheetBehavior,但在覆盖的onLayoutChild内设置状态.下面提供了代码.
The solution is to provide our own class which extends BottomSheetBehavior, but setting the state inside an overridden onLayoutChild. The code is provided below.
uk/ac/qub/quibe/misc/ExpandedBottomSheetBehavior.java
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
*/
}
}
在布局文件参考中,请参考您的新自定义行为.
In the layout file reference reference your new custom behavior.
更改
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
收件人
app:layout_behavior="uk.ac.qub.quibe.misc.ExpandedBottomSheetBehavior"
这篇关于NullPointerException与AppCompat BottomSheets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!