简体   繁体   English

在Fragment中使用RecyclerView时,Android出现Kotlin错误

[英]Android with Kotlin error when use RecyclerView in Fragment

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) 进程:com.example.yudha.kotlinauth,PID:16435 java.lang.IllegalStateException:video_recyclerview在com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView(VideoFragment.kt:30)中不能为null

RecyclerView Adapter: 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){

}

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: 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): Recycler视图(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>

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
    }

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. 只需将video_recyclerview更改为rootView.video_recyclerview因为在片段中您需要先知道您使用的视图在哪里。

you forgot to initialize the the recyclerview. 你忘了初始化recyclerview。

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

Update Your code with below. 使用以下更新您的代码。

RecyclerView Adapter: 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
}

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

When you add this to your app.grable file you can direct access to your component`s by id 将此添加到app.grable文件后,您可以通过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 现在一切都会像魅力一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM