我正在尝试实现模式底部表单,让用户选择相机/图库/删除选项,以便上传/删除个人资料图片。
底部工作表显示没有任何问题。但是当用户单击任何选项时 - 会引发下面提到的错误。
Attempt to invoke interface method 'ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)' on a null object reference
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 类( fragment )
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 被调用( View 分配给 fragment )之后初始化监听器。您在分配给底部工作表 fragment 的布局之前初始化监听器。
试试这个
`
公共(public)类 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);
} `
关于java - 尝试对空对象引用调用接口(interface)方法 'ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58317172/