在 Kotlin Android 项目中,FragmentTransaction 无法编译

4
在学习Kotlin和Android的过程中,编译失败和错误信息不够明确让我很困惑。错误信息如下:

无法使用所提供的参数调用以下任何一个函数。 add(Fragment!, String!)在android.app.FragmentTransaction中定义 add(Int, Fragment!)在android.app.FragmentTransaction中定义

在两个实例中,Fragment!都被标红了。我知道Kotlin使用!来指代Java类,但我无法理解为什么它不能接受我提供的输入方式。
非常感谢您的帮助。
    fun displayEditRoutine(){

    //Set our variables
    var ft = fragmentManager.beginTransaction()

    //Basic "newInstance" constructor to avoid omitting necessary variables
    var frag = EditRoutine.newInstance(mRoutineID,this)

    //Here is where error occurs
    ft.add(R.id.are_container, frag).commit()

}

被引用的EditRoutine类:
class EditRoutine : Fragment() {

//Variables
private var mRoutineID: String? = null
private var mListener: OnEditRoutineFragmentListener? = null

//Views
@BindView(R.id.fer_routineName) internal var vRoutine: TextInputEditText? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (arguments != null) {
        mRoutineID = arguments.getString(Keys.b_RoutineID)
    }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val v = inflater.inflate(R.layout.fragment_edit_routine, container, false)
    ButterKnife.bind(activity)
    return v
}

// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(): Unit{
    if (mListener != null && vRoutine!!.text.toString() != "") {
        val contentValues = ContentValues()
        contentValues.put(Routine.Table.KEY_NAME, vRoutine!!.text.toString())

        //Pass the values into the interface
        mListener!!.onDoneClicked(contentValues)
    }
}

override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is OnEditRoutineFragmentListener) {
        mListener = context
    } else {
        throw RuntimeException(context.toString() + " must implement OnEditRoutineFragmentListener")
    }
}

override fun onDetach() {
    super.onDetach()
    mListener = null
}

//Internal Methods


//Interface
interface OnEditRoutineFragmentListener {
    // TODO: Update argument type and name
    fun onDoneClicked(cv: ContentValues)

}

companion object {

    /**
     * @param routineID = passed ID. If null, don't load content values
     * *
     * @return A new instance of fragment EditRoutine.
     */
    fun newInstance(routineID: String, ctx: Context): EditRoutine {
        val fragment = EditRoutine()
        val args = Bundle()
        args.putString(Keys.b_RoutineID, routineID)
        fragment.arguments = args
        return fragment
    }
}

3
如果您正在创建支持片段,应该使用 supportFragmentManager 而不是 fragmentManager。 - BladeCoder
你能发一下你的 EditRoutine 类吗? - user961376
谢谢您的建议。我的最小API是19,所以我正在使用常规片段。 - Josh Ribeiro
EditRoutine已添加。 - Josh Ribeiro
你在 build.gradle 文件中应用了 Android-Kotlin 插件吗? - erluxman
3个回答

8
尝试这样做: ft.add(R.id.container_all, frag as Fragment).commit()

那个可行,我猜 Kotlin 没有看到我的类的父类。 - Josh Ribeiro

0

JvmStatic:这个注解是必需的(文档

伴生对象 {

/**
 * @param routineID = passed ID. If null, don't load content values
 * *
 * @return A new instance of fragment EditRoutine.
 */

@JvmStatic 
fun newInstance(routineID: String, ctx: Context): EditRoutine {
    val fragment = EditRoutine()
    val args = Bundle()
    args.putString(Keys.b_RoutineID, routineID)
    fragment.arguments = args
    return fragment
}

}


0

将片段强制转换为Fragment的答案对我没有帮助,它仍然无法编译。

因此,我采用了BladeCoder的建议,并将fragmentManager替换为supportFragmentManager

fun displayEditRoutine(){

    //Set our variables
    var ft = supportFragmentManager.beginTransaction()

    //Basic "newInstance" constructor to avoid omitting necessary variables
    var frag = EditRoutine.newInstance(mRoutineID,this)

    //Here is where error occurs
    ft.add(R.id.are_container, frag).commit()

}

感谢BladeCoder的所有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接