我尝试在BottomSheetDialogFragment
中添加一个行为来使其不可关闭,但是它抛出了以下错误:
Caused by: java.lang.NullPointerException: 尝试在空对象上调用虚拟方法'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()'
你认为可能是什么问题?
你有任何想法或教程如何解决这个问题,或者有些东西可以详细地教我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");
}