开发者社区> 问答> 正文

尝试在空对象引用上调用接口方法

我正在尝试实现 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(); }

}

展开
收起
小六码奴 2019-10-10 15:20:49 944 0
1 条回答
写回答
取消 提交回答
  • 尝试这个 :

    @Override public void onAttach(Context context) { super.onAttach(context); try { bottomSheetListener = (BottomSheetListener) getActivity(); }catch (ClassCastException e) { throw new ClassCastException(context.toString()+"must implement BottomSheetListener"); } }

    2019-10-10 15:22:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
对象的生命期管理 立即下载
低代码开发师(初级)实战教程 立即下载