Я пытаюсь реализовать модальный нижний лист, чтобы позволить пользователю выбрать камеру / галерею / удалить параметры для загрузки / удаления изображения профиля.
Нижний лист отображается без каких-либо проблем. Но когда пользователь нажимает на любойoption - ниже указана ошибка.
Попытка вызвать метод интерфейса '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();
}
}
Заранее спасибо.