Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以从自定义视图调用DialogFragment?_Android_Android Fragments_Android Custom View - Fatal编程技术网

Android 是否可以从自定义视图调用DialogFragment?

Android 是否可以从自定义视图调用DialogFragment?,android,android-fragments,android-custom-view,Android,Android Fragments,Android Custom View,我已尝试从自定义视图调用DialogFragment: DetailsDialogFragment .newInstance(newSelectedDate, adapterItems[newPosition].progress) .apply { show(childFragmentManager, SCORE_DETAILS_DIALOG_TAG) } 其中DetailsDialogFragment如下所示: class DetailsDialog

我已尝试从自定义视图调用DialogFragment:

DetailsDialogFragment
    .newInstance(newSelectedDate, adapterItems[newPosition].progress)
    .apply {
         show(childFragmentManager, SCORE_DETAILS_DIALOG_TAG)
    }
其中
DetailsDialogFragment
如下所示:

class DetailsDialogFragment : AppCompatDialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return requireActivity().let {
            val dialog = Dialog(requireContext(), R.style.CustomDialog)
            dialog.window?.setDimAmount(BaseDialogFragment.SCRIM_OPACITY)
            dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            dialog
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_details_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.rootView.apply {
            findViewById<TextView>(R.id.titleTextView).text = arguments?.getString(ARGS_MONTH)
            findViewById<ActionButton>(R.id.button).setOnClickListener {
                dismiss()
            }
            findViewById<ImageView>(R.id.closeImageView).setOnClickListener {
                dismiss()
            }
        }
    }

    companion object {
        fun newInstance(
            month: String,
            score: Int
        ): DetailsDialogFragment {
            return DetailsDialogFragment()
                .apply {
                    arguments = bundleOf(
                        ARGS_MONTH to month,
                        ARGS_SCORE to score
                    )
                }
        }
    }
}

可以从自定义视图调用
DialogFragment
吗?

出现此异常的原因是您试图使用新创建实例的
子碎片管理器
,这当然是不可能的,因为DialogFragment的内部尚未初始化(包括其
childFragmentManager

如果您使用的是AndroidX,我会在自定义视图中使用扩展方法,并尝试执行以下操作:

在自定义视图中

val dialogFragment = DetailsDialogFragment
    .newInstance(newSelectedDate, adapterItems[newPosition].progress)

dialogFragment.show(findFragment().childFragmentManager, SCORE_DETAILS_DIALOG_TAG)

val dialogFragment = DetailsDialogFragment
    .newInstance(newSelectedDate, adapterItems[newPosition].progress)

dialogFragment.show(findFragment().childFragmentManager, SCORE_DETAILS_DIALOG_TAG)