gpt4 book ai didi

android - 如何将 BottomSheetBehavior 与 BottomSheetDialogFragment 集成

转载 作者:行者123 更新时间:2023-11-29 18:34:14 24 4
gpt4 key购买 nike

我尝试在 BottomSheetDialogFragment 中获取一个 Behavior,因此我会使其不可关闭,但它会抛出此错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference

你认为它可能是什么?你有任何想法或教程如何解决这个问题。或者可以详细教我 BottomSheetDialogFragments 的内容。

代码:

public class BottomSheetExcample extends BottomSheetDialogFragment {

private BottomSheetBehavior bottomSheetBehavior;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_sheet, container, false);

view.findViewById(R.id.txt1);
view.findViewById(R.id.txt2);

bottomSheetBehavior = BottomSheetBehavior.from();


return view;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
View view = View.inflate(getContext(), R.layout.bottom_sheet, null);

TextView txt1 = view.findViewById(R.id.txt1);
TextView txt2 = view.findViewById(R.id.txt2);

dialog.setContentView(view);
bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
return dialog;
}

@Override
public void onStart() {
super.onStart();
bottomSheetBehavior.setHideable(false);
bottomSheetBehavior.setPeekHeight(500, true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BottomSheetExcample b = new BottomSheetExcample();
b.show(getSupportFragmentManager(), "bse");


}

最佳答案

移除 onCreateView 方法

    public class BottomSheetExample extends BottomSheetDialogFragment {

private BottomSheetBehavior bottomSheetBehavior;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
View view = View.inflate(getContext(), R.layout.fragment_cart, null);


dialog.setContentView(view);
bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
return dialog;
}

@Override
public void onStart() {
super.onStart();
bottomSheetBehavior.setHideable(false);
bottomSheetBehavior.setPeekHeight(500);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

}

关于android - 如何将 BottomSheetBehavior 与 BottomSheetDialogFragment 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com