我正在 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.AppCompat
或 Theme.AppCompat.Light
。
关于android - Kotlin fragment 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244238/