本文介绍了未附加 AlertDialog 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MyAlertDialog 在尝试为侦听器设置上下文时抛出 ClassCastException.我正在从片段调用 MyAlertDailog.

MyAlertDialog throws ClassCastException when trying to set the context to the listener. I'm calling the MyAlertDailog from a fragment.

我正在使用 android 开发文档中的指南.

I'm using the guide found in the android dev docs.

https://developer.android.com/guide/topics/ui/dialogs#PassingEvents

我的片段

class MyFragment : Fragment(), MyAlerDialog.MyAlertDialogListener {

    ...

    fun launchAlertDialog() {
        val dailog = MyAlertDialog().also {
            it.show(requireActivity().supportFragmentManager, "DialogInfoFragment")
        }
    }

    override fun onDialogPostiveCLick(dialog: DialogFragment) {
        Log.i(TAG, "Listener returns a postive click")
    }
}

MyAlertDialog

MyAlertDialog

class MyAlertDialog : DialogFragment() {

    // Use thsi instance for the interface
    internal var listener: MyAlertDialogListener

    // My Fragment implements this interface.
    interface MyAlertDialogListener {
        onDialogPositiveClick(dialog: DialogFragment)
    }


    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Verify  that the host implements the callback.
        try {
            // My problem is here.
            listener = context as MyAlertDailog
        } catch (e: ClassCastException) {
            // exception thrown here.
            throw ClassCastException((context.toString() + " must implement MyAlertDailogListener")
        }
    }

    override fun onCreateDialog(savedInstanceState:Bundle?): Dialog {
        return activity?.let {
             val builder = AlertDialog.builder(it)
             ...
             builder.setPosiviveButton("Positive button",
               DialogInterface.OnClickListener {
                   listener.onDialogPositiveClick(this)
             }
        }
    }

}

错误报告

    java.lang.ClassCastException: com.example.androiddevpractice.MainActivity@ab56136 must implement MyAlertDialogListener
        at com.example.androiddevpractice.topics.userinterface.dialog.MyAlertDialog.onAttach(MyAlertDialog.kt:35)

推荐答案

即使 launchAlertDialog() 方法在 MyFragment 中,host"也没有MyAlertDialog 是您的 Activity,而不是 MyFragment.

Even though the launchAlertDialog() method is inside of MyFragment, the "host" for MyAlertDialog is your Activity, not MyFragment.

MainActivity 内部实施 MyAlerDialog.MyAlertDialogListener 以使转换成功.如果需要,MainActivity 然后可以与 MyFragment 通信.

Implement MyAlerDialog.MyAlertDialogListener inside of MainActivity in order for the cast to succeed. MainActivity can then communicate to MyFragment if it has to.

或者,您可以使用 setTargetFragment() 来连接"MyFragment 和 MyAlertDialog 直接:

Alternatively, you could use setTargetFragment() in order to "connect" MyFragment and MyAlertDialog directly:

val dialog = MyAlertDialog()
dialog.setTargetFragment(this, 0)
dialog.show(requireActivity().supportFragmentManager, "DialogInfoFragment")

然后,不是覆盖 onAttach() 并转换上下文,而是转换 getTargetFragment() 的结果:

Then, rather than overriding onAttach() and casting a context, you would cast the results of getTargetFragment():

builder.setPosiviveButton(
        "Positive button",
        DialogInterface.OnClickListener {
            val listener = targetFragment as MyAlertDialogListener
            listener.onDialogPositiveClick(this)
        }
)

这篇关于未附加 AlertDialog 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:41