FragmentTransaction won't Compile in Kotlin Android Project

孤者浪人 提交于 2019-12-14 01:31:01

问题


In the process of learning Kotlin with Android, the failure to compile and generally unhelpful error text have left me stumped. The error text says the following:

None of the following functions can be called with the arguments supplied. add(Fragment!, String!) defined in android.app.FragmentTransaction add(Int, Fragment!) defined in android.app.FragmentTransaction

In both instances the Fragment! text is highlighted in red. I am aware that Kotlin refers to Java classes with an !, but I can't seem to understand why it is not happy with the way in which I provided the inputs.

Any insight would be greatly appreciated.

    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()

}

The EditRoutine class being referenced:

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
    }
}

回答1:


Try this: ft.add(R.id.container_all, frag as Fragment).commit()




回答2:


JvmStatic : this annotation is required (documentation)

companion object {

/**
 * @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
}

}



来源:https://stackoverflow.com/questions/44507770/fragmenttransaction-wont-compile-in-kotlin-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!