Android with Kotlin error when use RecyclerView in

2019-06-28 05:48发布

问题:

I have error said this:

Process: com.example.yudha.kotlinauth, PID: 16435 java.lang.IllegalStateException: video_recyclerview must not be null at com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView(VideoFragment.kt:30)

RecyclerView Adapter:

    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){

}

Fragment Activity:

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>

Recycler view(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>

回答1:

You have forgot to do:

video_recyclerview = rootView.findViewById(R.id.id_of_video_recyclerview) as RecyclerView

So the code be like:-

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
    }


回答2:

I have to solve this problem

just change video_recyclerview to rootView.video_recyclerview because in fragment you need to know first where is the view you use.



回答3:

you forgot to initialize the the recyclerview.

video_recyclerview = rootView.findViewById(R.id.video_recyclerview) as RecyclerView
video_recyclerview.layoutManager = LinearLayoutManager(activity)
video_recyclerview.adapter = MainAdapter()


回答4:

Update Your code with below.

RecyclerView Adapter:

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
}

Fragment Activity:

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


回答5:

When you add this to your app.grable file you can direct access to your component`s by id

    apply plugin: 'kotlin-android-extensions'

Then import this in your fragment

import kotlinx.android.synthetic.main.fragment_video.*

Now everything will work like a charm