android - 片段上下文中的错误(oncreatedialog)
问题描述
我有一个扩展 BottomSheetDialogFragment 的片段。运行应用程序时出现此错误。我这个问题出在:mContext = getActivity().getApplicationContext();
我试图用它替换它,mContext = getActivity();
但它再次显示错误。
这是代码:
public class FragmentBottomSheetDialogFull extends BottomSheetDialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
final View view = View.inflate(getContext(), R.layout.aa, null);
dialog.setContentView(view);
mBehavior = BottomSheetBehavior.from((View) view.getParent());
mBehavior.setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO);
app_bar_layout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
lyt_profile = (LinearLayout) view.findViewById(R.id.lyt_profile);
mSoundCheckBox = view.findViewById(R.id.sound_checkbox);
mVibrationCheckBox = view.findViewById(R.id.vibration_checkbox);
populateSoundContents();
populateVibrationContents();
mContext = getActivity().getApplicationContext();
AppController.currentActivity = getActivity();
((TextView) view.findViewById(R.id.name)).setText(R.string.setting_heading);
((TextView) view.findViewById(R.id.name_toolbar)).setText(R.string.setting_heading);
((View) view.findViewById(R.id.lyt_spacer)).setMinimumHeight(Utils.getScreenHeight() / 2);
hideView(app_bar_layout);
return dialog;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void switchSoundCheckbox() {
isSoundOn = !isSoundOn;
SettingsPreferences.setSoundEnableDisable(mContext, isSoundOn);
populateSoundContents();
}
protected void populateSoundContents() {
if (SettingsPreferences.getSoundEnableDisable(mContext)) {
mSoundCheckBox.setChecked(true);
} else {
mSoundCheckBox.setChecked(false);
}
isSoundOn = SettingsPreferences.getSoundEnableDisable(mContext);
}
public void viewClickHandler(View view) {
switch (view.getId()) {
case R.id.sound_checkbox:
switchSoundCheckbox();
break;
}
}
解决方案
populateSoundContents()
设置mCotext
变量后调用。请更改您的代码如下:
mContext = getActivity().getApplicationContext();
populateSoundContents();
populateVibrationContents();
推荐阅读
- java - Cas服务器和Web服务认证(服务票证)
- javascript - 获取 DropdownItem 的标签或值以将文本值设置为 DropdownToggle
- java - 致命异常:主要,膨胀类片段时出错,android.view.InflateException
- git - git:将两个分支与一个分支中的秘密文件合并
- python - Strip() 的正则表达式版本 - 使用 Python 自动化无聊的东西
- javascript - 如何从文本中过滤数字
- reactjs - 如何在 render() 中的 map() 中创建和分配变量?
- c# - 根据第一个 ComboBox (1) c# 的输入,从 ComboBox (2) 中消除一个选项
- jquery - 我在 Container/section div 中有 2 个 div,但是两个 div 之一被推出容器 div,为什么?
- python - 删除符号时如何避免在csv中引用?