在使用Android学习Kotlin的过程中,编译失败以及通常无用的错误文本让我感到难过。错误文本说明如下:
使用参数不能调用以下任何函数 提供。 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
}
}
答案 0 :(得分:6)
试试这个:
ft.add(R.id.container_all, frag as Fragment).commit()
答案 1 :(得分:0)
JvmStatic:此注释是必需的(documentation)
伴侣对象{
/**
* @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
}
}
答案 2 :(得分: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