我正在尝试实现 modal bottom sheet,以允许用户选择相机/图库/删除选项,以便上传/删除个人资料图片。
Bottom sheet显示没有任何问题。但是,当用户单击任何选项时-抛出以下提到的错误。
尝试在空对象引用上调用接口方法'ImageChooserDialog $ BottomSheetListener.onOptionsClicked(java.lang.String)'
ImageChooserDialog.java类(Modal bottom sheet):
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(); }
}
尝试这个 :
@Override public void onAttach(Context context) { super.onAttach(context); try { bottomSheetListener = (BottomSheetListener) getActivity(); }catch (ClassCastException e) { throw new ClassCastException(context.toString()+"must implement BottomSheetListener"); } }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。