編譯使用Android,未能編譯和一般無益的錯誤的文字給我留下難倒學習科特林的過程。錯誤文字說明如下:FragmentTransaction不會在科特林的Android項目
以下函數都不能用提供的參數 調用。添加(片段!字符串!)在 定義android.app.FragmentTransaction添加(中間體,片段!)在 android.app.FragmentTransaction
定義在這兩種情況下,片段!文字以紅色突出顯示。我意識到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
}
}
如果你正在創建支持片段,你應該使用supportFragmentManager而不是fragmentManager – BladeCoder
你可以發佈你的'EditRoutine'類嗎? – Marce
感謝您的建議。我分API爲19,所以我用常規的片段 –