java - 尝试对空对象引用调用接口(interface)方法 'ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)'

标签 java android android-studio android-fragments

我正在尝试实现模式底部表单,让用户选择相机/图库/删除选项,以便上传/删除个人资料图片。

底部工作表显示没有任何问题。但是当用户单击任何选项时 - 会引发下面提到的错误。

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/

相关文章:

java - 使用 Junit 比较文本文件

java - requestLocationUpdates 在错误的时间生成位置

java - 使用 cssSelector 或 xPath Selenium Web 驱动程序在过滤器上应用按钮?

java - 带有导入库的 Android Studio java.lang.NoSuchMethodError

java - 无法解析 org.xml.sax.SAXParseException : cvc-elt. 1:找不到元素 'soapenv:Envelope' 的声明

android - Coverflow Carousel 效果与 Android 中的 iPhone/iPad 相同

android - 使用 RN 和 expo 更改 android 上的导航栏?

Android Studio : Error: Activity not started, 未知错误代码 5

java - 错误 : no suitable constructor after update Android Studio from 0. 6.1 到 0.8.9

android - 以编程方式更改 CardView 的背景颜色