java - 尝试在空对象引用上调用接口方法“ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)”
问题描述
我正在尝试实现模态底页,让用户选择相机/画廊/删除选项以上传/删除个人资料图片。
底部工作表显示没有任何问题。但是当用户单击任何选项时 - 抛出下面提到的错误。
尝试在空对象引用上调用接口方法“ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)”
ImageChooserDialog.java 类(模态底页):
public class ImageChooserDialog extends BottomSheetDialogFragment {
CircularImageView camera,gallery,remove;
public static final String TAG = "ImageChooserDialog";
private BottomSheetListener bottomSheetListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_choose_dialog, container, false);
camera = v.findViewById(R.id.imagechooser_camera);
gallery = v.findViewById(R.id.imagechooser_gallery);
remove = v.findViewById(R.id.imagechooser_remove);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Camera");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
}
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Gallery");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
}
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Remove");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
}
}
});
return v;
}
@Override
public int getTheme() {
return R.style.BottomSheetDialogTheme;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
bottomSheetListener = (BottomSheetListener) getParentFragment();
}catch (ClassCastException e)
{
throw new ClassCastException(context.toString()+"must implement BottomSheetListener");
}
}
public interface BottomSheetListener{
void onOptionsClicked(String option);
}
}
EditProfile.java 类(片段)
public class EditProfile extends Fragment implements ImageChooserDialog.BottomSheetListener {
//to open modal bottom sheet
private void openOptionsBox()
{
ImageChooserDialog fragment= new ImageChooserDialog();
fragment.show(getFragmentManager(),ImageChooserDialog.TAG);
}
@Override
public void onOptionsClicked(String option) {
Toast.makeText(getContext(), ""+option, Toast.LENGTH_SHORT).show();
}
}
提前致谢。
解决方案
您应该在调用 onCreateView 之后初始化侦听器(分配给片段的视图)。在分配给底部工作表片段的布局之前初始化侦听器。
尝试这个
`
公共类 ImageChooserDialog 扩展 BottomSheetDialogFragment {
CircularImageView camera,gallery,remove;
public static final String TAG = "ImageChooserDialog";
private BottomSheetListener bottomSheetListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_choose_dialog, container,
false);
return v;
}
@Override
public void onViewCreated ( @NonNull View view, @Nullable Bundle
savedInstanceState ) {
super.onViewCreated( view, savedInstanceState );
initView()
}
private fun initView()
{
camera = getView().findViewById(R.id.imagechooser_camera);
gallery = getView().findViewById(R.id.imagechooser_gallery);
remove = getView().findViewById(R.id.imagechooser_remove);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Camera");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
}
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Gallery");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
}
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bottomSheetListener != null)
{
bottomSheetListener.onOptionsClicked("Remove");
dismiss();
}else {
Toast.makeText(getActivity(), "Initialize",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getTheme() {
return R.style.BottomSheetDialogTheme;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
bottomSheetListener = (BottomSheetListener) getParentFragment();
}catch (ClassCastException e)
{
throw new ClassCastException(context.toString()+"must implement
BottomSheetListener");
}
}
public interface BottomSheetListener{
void onOptionsClicked(String option);
} `
推荐阅读
- c# - 实体框架的异步函数运行不正常
- java - 在 android 9.0 API 28 中创建浮动窗口
- video - ffmpeg - 偏移视频的裁剪部分
- android - Unity VR 如何正确对焦眼睛
- javascript - Uncaught SyntaxError: Unexpected identifier - 尝试理解 D3 js
- python - 在 jupyter ntbk 中运行 reg.predict 时遇到问题说“ValueError”
- r - dplyr mutate 将“No”转换为 0,将“Yes”转换为 1
- python - 是否有用于单调样条的 Python 包?
- git - 如何在主分支的第一个版本上创建 Github 拉取请求
- reactjs - 如何运行 React Native 应用程序 Web 浏览器