【问题标题】:FragmentTransaction won't Compile in Kotlin Android ProjectFragmentTransaction 不会在 Kotlin Android 项目中编译
【发布时间】:2017-06-12 19:59:30
【问题描述】:

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

【问题讨论】:

  • 如果你正在创建支持片段,你应该使用 supportFragmentManager 而不是 fragmentManager
  • 你能发布你的EditRoutine类吗?
  • 感谢您的建议。我的最小 API 是 19,所以我使用的是常规片段
  • EditRoutine 已添加
  • 您是否在 build.gradle 文件中应用了 android-kotlin 插件?

标签: android android-fragments kotlin fragmentmanager


【解决方案1】:

试试这个: ft.add(R.id.container_all, frag as Fragment).commit()

【讨论】:

  • 成功了,我猜 Kotlin 看不到我班级的家长
【解决方案2】:

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
}

}

【讨论】:

    【解决方案3】:

    将片段转换为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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多