首页 > 解决方案 > 我如何从 Fragment 调用 FragmentManager?科特林

问题描述

我正在构建警报应用程序并尝试打开时间选择器对话框。我需要在关闭后获取时间数据。问题出在.show()功能上。它在片段中不起作用。它说None of the following functions can be called with the arguments supplied.

我的课

class AlarmPickerDialog : DialogFragment() {
    override fun onCreateView(inflater: LayoutInflater?, container:
    ViewGroup?, savedInstanceState: Bundle?): View {

        var myView = inflater!!.inflate(R.layout.alarm_time_picker, container, false)

        return myView
    }
}

还有我的调用方法

val alarmDialog = AlarmPickerDialog()
val fm = getActivity()!!.getSupportFragmentManager()
alarmDialog.show(fm, "MyDialog")

也许有更好的方法来获取时间数据?

标签: androidkotlin

解决方案


问题:它似乎从而不是AlarmPickerDialog延伸。android.app.DialogFragmentandroid.support.v4.app.DialogFragment

解决方案:将代码更改为

// Comment-out this line
// import android.app.DialogFragment

// Add this line
import android.support.v4.app.DialogFragment

class AlarmPickerDialog : DialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var myView = inflater!!.inflate(R.layout.alarm_time_picker, container, false)
        return myView
    }
}

推荐阅读