wake-up-neo.com

Android mit Kotlin-Fehler bei Verwendung von RecyclerView in Fragment

Ich habe Fehler gesagt:

Prozess: com.example.yudha.kotlinauth, PID: 16435 Java.lang.IllegalStateException: video_recyclerview darf nicht null sein unter 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){

}

Fragmentaktivität:

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-Ansicht (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>
4
Yudha Patria

Sie haben vergessen zu tun: 

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

So lautet der Code: -

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
    }
6
Paresh P.

Ich muss dieses Problem lösen

Ändern Sie einfach video_recyclerview in rootView.video_recyclerview, da Sie in fragment zuerst wissen müssen, wo sich die verwendete Ansicht befindet.

3
Yudha Patria

sie haben vergessen, die Recyclingübersicht zu initialisieren.

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

Wenn Sie dies zu Ihrer app.grable-Datei hinzufügen, können Sie über die ID direkt auf Ihre Komponenten zugreifen

    apply plugin: 'kotlin-Android-extensions'

Dann importieren Sie dies in Ihr Fragment

import kotlinx.Android.synthetic.main.fragment_video.*

Jetzt wird alles wie ein Zauber wirken

0
Fr099y

Aktualisieren Sie Ihren Code mit unten.

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
}

Fragmentaktivität:

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
}
}
0
Ravi Vaghela