自定义对话框的侦听器为空

gopyfrb3  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(334)

我得到了一个nullpointerexception为我的听众在我的类,即使一切都应该正常工作。我在flow的教程中为自定义对话框编写了代码,重新编写了几次,并检查了源代码,没有遗漏任何内容。这是密码
从中调用对话框的片段

public class FragmentMain extends Fragment implements CustomDialog.DialogListener {

    .
    .
    .

    private int timeLimit = 0;
    private Button dialogOpen;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        .
        .
        .

        dialogOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogDashboard dialogDashboard = new DialogDashboard();
                dialogDashboard.show(getActivity().getSupportFragmentManager(), "notif_dialog");
            }
        }); //dialog called here

        return view;
    }

    //getting text from spinner in dialog
    @Override
    public void applyText(String time) {
        switch (time) {
            case "30 Minutes":
                timeLimit = 30;
            case "1 Hour":
                timeLimit = 60;
            case "2 Hours":
                timeLimit = 120;
            case "4 Hours":
                timeLimit = 240;
            case "5 Hours":
                timeLimit = 300;
        }
    }
}

对话框代码:

public class CustomDialog extends AppCompatDialogFragment {

    private Button confirmButton;
    private Spinner spinner;
    private DialogListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
                R.style.NotificationAlertDialog);

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.custom_dialog_notif, null);

        confirmButton = view.findViewById(R.id.dialogButton);
        spinner = view.findViewById(R.id.dialogSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.spinner_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = spinner.getSelectedItem().toString();
                listener.applyText(time); // says listener is null when I just do e.printStackTrace();
                dismiss();
            }
        });

        builder.setView(view)
                .setCancelable(true);

        return builder.create();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            listener = (DialogListener) context;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassCastException(context.toString() +
                    "must implement dialog listener"); // this says if the listener is null, but even if it's implemented it returns null...
        }
    }

    public interface DialogListener {
        void applyText(String time);
    }

}
fquxozlt

fquxozlt1#

根本原因
你得了npe是因为 onAttach() 当片段附加到其主机/活动时调用回调。这个 context param是活动本身,而不是fragmentmain。
解决方案
第一步:在fragmentmain中单击“打开对话框”按钮。

CustomDialog dialogDashboard = new CustomDialog();
dialogDashboard.show(getChildFragmentManager(), "notif_dialog");

步骤2:在customdialog中修改代码
删除此代码

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    try {
        listener = (DialogListener) context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new ClassCastException(context.toString() +
                "must implement dialog listener"); 
    }
}

当用户单击确认按钮时。

String time = spinner.getSelectedItem().toString();
Fragment fragment = getParentFragment();
if (fragment instanceof CustomDialog.DialogListener) {
    listener = (DialogListener) fragment;
    listener.applyText(time);
}
dismiss();

相关问题