FragmentTransaction无法在Kotlin Android项目中编译 [英] FragmentTransaction won't Compile in Kotlin Android Project
问题描述
在使用Android学习Kotlin的过程中,编译失败以及通常无用的错误文本使我感到困惑.错误文本显示以下内容:
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:
以下任何一个函数都不能通过参数调用提供.在以下位置定义的add(Fragment !, String!)android.app.FragmentTransaction add(Int,Fragment!)在android.app.FragmentTransaction
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
在两种情况下, Fragment!文本均以红色突出显示.我知道Kotlin用!来引用Java类,但是我似乎无法理解为什么它对我提供输入的方式不满意.
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.
任何见识将不胜感激.
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类:
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
}
}
推荐答案
尝试一下: ft.add(R.id.container_all,片段为片段).commit()
这篇关于FragmentTransaction无法在Kotlin Android项目中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!