android - Kotlin fragment 类型不匹配

标签 android android-fragments kotlin

我正在 Kotlin 中进行主/细节流 Activity 。我遇到 fragment 不匹配问题,因为在一项 Activity 中我需要 android.support.v4.app.Fragment ,而在其他 Activity 中我需要 android.app.Fragment 。我实现了自己的详细信息 fragment 并从 android.support.v4.app.Fragment 继承,以修复 ListActivity 中 supportFragmnet 事务中的不匹配

if (mTwoPane) {
    val fragment = GraphDetailFragment().apply {
    arguments = Bundle().apply {
        putString(GraphDetailFragment.ARG_ITEM_ID, item.id)
    }
}
supportFragmentManager
        .beginTransaction()
        .replace(R.id.graph_detail_container, fragment)
        .commit()
}

但是我的fragmentManager 与DetailActivity 不匹配

val fragment = GraphDetailFragment().apply {
            arguments = Bundle().apply {
                putString(GraphDetailFragment.ARG_ITEM_ID,
                        intent.getStringExtra(GraphDetailFragment.ARG_ITEM_ID))
            }
        }

fragmentManager.beginTransaction()
        .add(R.id.graph_detail_container, fragment)
        .commit()

控制台抛出:

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

有什么简单的方法可以解决这个问题吗?

最佳答案

使DetailActivity扩展AppCompatActivity,然后在其中使用supportFragmentManager

您还需要修复 styles.xml 中的 AppTheme。确保 parent 属性指向 Theme.AppCompatTheme.AppCompat.Light

关于android - Kotlin fragment 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244238/

相关文章:

android - 我可以通过函数参数发送扩展函数吗

firebase - Kotlin Firebase:无法将java.lang.String类型的对象转换为com.example.Model.perizinan类型

android - ListView getChildAt() 保持为空,但如果在 onclick 中有效

java - 如何在Activity中访问 fragment 的ViewPager View ?

java - Android 接收 null Parcelable 对象

android - GetItem(int) 在 FragmentPagerAdapter 中返回 Listfragment

android - 在 Jetpack Compose 中将高度设置为 Row 相当于 xml 中的wrap_content

java - 通过纯原生 Android 应用检索 android.os.Build.... 系统属性

android - 如何从其市场链接中解析android应用程序的基本信息?

android - 迁移到房间 : How to do Full Text Search?