带有 AppCompat BottomSheets 的 NullPointerException [英] NullPointerException with AppCompat BottomSheets

查看:16
本文介绍了带有 AppCompat BottomSheets 的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"

这篇关于带有 AppCompat BottomSheets 的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆