android - 处理dialogFragment按钮点击

标签 android fragment android-dialogfragment

我的应用程序中有 Activity,其中几乎没有 fragment 。其中一个 fragment 有一个 DialogFragment,它通过单击按钮来调用。 DialogFragment 有 3 个按钮 - 积极、消极和中性。

public class CompanyNotConnectedToSRDialog extends DialogFragment {

    public static final String TAG = CompanyNotConnectedToSRDialog.class.getSimpleName();

    private NotConnectedDialogListener mNotConnectedDialogListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity(), R.style.DefaultAlertDialogTheme)
                .setTitle("Register")
                .setMessage("Do you really want to register?")
                .setNeutralButton("Skip", (dialog1, which) -> {
                    mNotConnectedDialogListener.onSkipBtnNotConnectedDialogPressed();
                })
                .setNegativeButton("Cancel", null)
                .setPositiveButton("Register", (dialog12, which) -> {
                    mNotConnectedDialogListener.onSendBtnNotConnectedDialogPressed();
                })
                .create();
    }


    public interface NotConnectedDialogListener {
        void onSkipBtnNotConnectedDialogPressed();
        void onSendBtnNotConnectedDialogPressed();
    }

    public void setListener(NotConnectedDialogListener listener) {
        this.mNotConnectedDialogListener = listener;
    }

正如您所看到的,我创建了公共(public)接口(interface),其中包含用于跳过和注册按钮的两个方法(取消按钮监听器为空,因此无关紧要)以及该监听器的 Setter。 然后我在调用这个dialogFragment的 fragment 中实现了这个接口(interface),我重写了方法并调用dialogFragment,如下所示:

if (mNotConnectedDialog == null) {
            mNotConnectedDialog = new CompanyNotConnectedToSRDialog();
            mNotConnectedDialog.setListener(this);
            mNotConnectedDialog.show(getActivity().getFragmentManager(), CompanyNotConnectedToSRDialog.TAG);
        } else {
            mNotConnectedDialog.show(mActivity.getFragmentManager(), CompanyNotConnectedToSRDialog.TAG);
            mNotConnectedDialog.setListener(this);
        }

问题是,如果我按父 Fragment 中的按钮来显示 DialogFragment、旋转屏幕并按 DialogFragment 中的任何按钮,我会收到 NullPointerException,因为我的监听器为空。

java.lang.NullPointerException: Attempt to invoke interface method 'void com.myapp.ui.object.create.dialogs.CompanyNotConnectedToSRDialog$NotConnectedDialogListener.onSendBtnNotConnectedDialogPressed()' on a null object reference

at com.myapp.ui.object.create.dialogs.CompanyNotConnectedToSRDialog.lambda$onCreateDialog$1(CompanyNotConnectedToSRDialog.java:31)

如果此解决方案错误,如何处理这些点击并设置监听器?

PS:请不要告诉我有关 android:configChanges 的事情。

最佳答案

所以当前的解决方案不起作用,因为当您旋转时,对话框 fragment 会被破坏并重新创建。所以 setListener 没有被调用。解决方案取决于您的监听器是 Activity 还是另一个 fragment 。

如果它是一个 Activity ,您可以覆盖 DialogFragment 中的 onAttach 并在那里设置监听器。如果您的监听器是一个 fragment ,那么在 OnCreateDialog 方法中,您可以通过标记查找 fragment 并以这种方式设置监听器。例如。

Fragment listenerFragment = getActivity().getSupportFragmentManager().findFragmentByTag(getString(R.string.your_listener_fragment_tag));

if( listenerFragment instanceOf NotConnectedDialogListener ) {
    listener = (NotConnectedDialogListener) listenerFragment;
} else {
    //Handle what to do if you don't have a listener here. Maybe dismiss the dialog. 
}

关于android - 处理dialogFragment按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46000022/

相关文章:

java - 关闭对话框后运行方法 - Android

java - Android 应用程序可以在手机上运行,​​但不能在 AVD 上运行

php - 在 php 中循环遍历 JSONArray 的值

php - 从 php 数据库中获取一组 JSON 数据

android - 如果从 fragment 调用 DialogFragment 抛出 ClassCastException

android - ViewPager fragment 中的 CircularFloatingActionMenu

android - fragment 在进入后台后崩溃

android - Activity 暂停并重新创建后,如何停止 Activity 附加的 AlertDialog 继续出现在 Activity 上?

android - 在Kotlin项目中应用Realm插件导致编译报错

android - 在 DialogFragment 中显示 ConstraintLayout