J'ai l'erreur dit ceci:
Processus: exemple.yudha.kotlinauth, identifiant PID: 16435 Java.lang.IllegalStateException: video_recyclerview ne doit pas être null sur com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView (VideoFragment.kt: 30)
Adaptateur RecyclerView:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){
val videoTitles = listOf("First title", "Second", "3rd", "Moore Title")
//number of item
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
//create view
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val videoTitle = videoTitles.get(position)
holder?.view?.video_title.text= videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
}
Activité du fragment:
class VideoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
}
myFragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical"
xmlns:app="http://schemas.Android.com/apk/res-auto"
tools:context=".fragments.VideoFragment">
<Android.support.v7.widget.RecyclerView
Android:id="@+id/video_recyclerview"
Android:layout_width="match_parent"
Android:layout_height="wrap_content" />
</LinearLayout>
Vue du recycleur (videorow.xml):
<?xml version="1.0" encoding="utf-8"?>
<Android.support.constraint.ConstraintLayout 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"
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<ImageView
Android:id="@+id/imageView2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginEnd="8dp"
Android:layout_marginStart="8dp"
Android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
Android:id="@+id/video_title"
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_marginEnd="8dp"
Android:layout_marginStart="8dp"
Android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</Android.support.constraint.ConstraintLayout>
Vous avez oublié de faire:
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView
Donc, le code soit comme: -
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView // Add this
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
Je dois résoudre ce problème
remplacez simplement video_recyclerview
par rootView.video_recyclerview
car dans le fragment, vous devez d’abord savoir où se trouve la vue que vous utilisez.
vous avez oublié d'initialiser le recyclerview.
video_recyclerview = rootView.findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
Lorsque vous ajoutez ceci à votre fichier app.grable, vous pouvez accéder directement à votre composant par son identifiant.
apply plugin: 'kotlin-Android-extensions'
Puis importez ceci dans votre fragment
import kotlinx.Android.synthetic.main.fragment_video.*
Maintenant tout va fonctionner comme un charme
Mettez à jour votre code avec ci-dessous.
Adaptateur RecyclerView:
class MainAdapter : RecyclerView.Adapter<CustomViewHolder>(){
val videoTitles = listOf("First title", "Second", "3rd", "Moore Title")
//number of item
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
//create view
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val videoTitle = videoTitles.get(position)
holder?.view?.video_title.text= videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
val video_title= view.video_title
}
Activité du fragment:
class VideoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_video, container, false)
video_recyclerview = findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()
return rootView
}
}