未附加 AlertDialog 侦听器
AlertDialog Listener not attached
MyAlertDialog 在尝试将上下文设置为侦听器时抛出 ClassCastException。我正在从片段调用 MyAlertDailog。
我正在使用 android 开发文档中的指南。
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
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
内部,MyAlertDialog
的“宿主”是您的 Activity , 不是 MyFragment
.
在 MainActivity
内实施 MyAlerDialog.MyAlertDialogListener
以使转换成功。如果需要,MainActivity 可以与 MyFragment
通信。
或者,您可以使用 setTargetFragment()
直接“连接”MyFragment 和 MyAlertDialog:
val dialog = MyAlertDialog()
dialog.setTargetFragment(this, 0)
dialog.show(requireActivity().supportFragmentManager, "DialogInfoFragment")
然后,不是重写 onAttach()
并转换上下文,而是转换 getTargetFragment()
:
的结果
builder.setPosiviveButton(
"Positive button",
DialogInterface.OnClickListener {
val listener = targetFragment as MyAlertDialogListener
listener.onDialogPositiveClick(this)
}
)
MyAlertDialog 在尝试将上下文设置为侦听器时抛出 ClassCastException。我正在从片段调用 MyAlertDailog。
我正在使用 android 开发文档中的指南。
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
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
内部,MyAlertDialog
的“宿主”是您的 Activity , 不是 MyFragment
.
在 MainActivity
内实施 MyAlerDialog.MyAlertDialogListener
以使转换成功。如果需要,MainActivity 可以与 MyFragment
通信。
或者,您可以使用 setTargetFragment()
直接“连接”MyFragment 和 MyAlertDialog:
val dialog = MyAlertDialog()
dialog.setTargetFragment(this, 0)
dialog.show(requireActivity().supportFragmentManager, "DialogInfoFragment")
然后,不是重写 onAttach()
并转换上下文,而是转换 getTargetFragment()
:
builder.setPosiviveButton(
"Positive button",
DialogInterface.OnClickListener {
val listener = targetFragment as MyAlertDialogListener
listener.onDialogPositiveClick(this)
}
)