错误文字:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sem.receivedata, PID: 21279
java.lang.ClassCastException: com.sem.receivedata.databinding.FragmentNameListBindingImpl cannot be cast to com.sem.receivedata.databinding.FragmentDescriptionBinding
at com.sem.receivedata.presentation.DescriptionFragment.onCreateView(DescriptionFragment.kt:28)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1670)
at androidx.fragment.app.BackStackRecord.commitNow(BackStackRecord.java:317)
at com.sem.receivedata.presentation.adapters.NameListAdapter$NameListHolder.bind$lambda-0(NameListAdapter.kt:58)
DescriptionFragment.kt:28
指向一个字符串binding = DataBindingUtil.inflate(inflater, R.layout.fragment_name_list, container, false)
NameListAdapter.kt:58
指着:
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.framelayout, fragment)
.commitNow()
NameListHolder
:
class NameListHolder(val binding: NameListItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(paginationLocalModel: PaginationLocalModel, position: Int, context: NameListFragment){
binding.name.text = paginationLocalModel.name
itemView.setOnClickListener{
val fragment = DescriptionFragment()
val bundle = Bundle()
bundle.putInt("position", position)
fragment.setArguments(bundle)
val activity=context.context as AppCompatActivity
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.framelayout, fragment)
.commitNow()
Log.d("OnClick", "произошло нажатие по позиции $position")
}
}
}
DescriptionFragment
:
class DescriptionFragment : Fragment() {
private var binding : FragmentDescriptionBinding? = null
private val descriptionViewModel : DescriptionViewModel by viewModel()
val position: Int = getArguments()?.getInt("position", 0) ?: 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_name_list, container, false)
binding?.name?.text = descriptionViewModel.loadDescription.value?.get(position)?.name
binding?.date?.text = descriptionViewModel.loadDescription.value?.get(position)?.date
binding?.description?.text = descriptionViewModel.loadDescription.value?.get(position)?.description
return binding?.root
}
}
第一个片段(包含 RecyclerView)在标记内运行Activity
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".presentation.MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/framelayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/rd_fragment"
android:name="com.sem.receivedata.presentation.NameListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
注意这些字符串:
你需要改变
在