首页 > 解决方案 > 尝试在空对象引用上调用接口方法“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();
}

 }

提前致谢。

标签: javaandroidandroid-studioandroid-fragments

解决方案


您应该在调用 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);
}    `

推荐阅读