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

Posted

技术标签:

【中文标题】在 Fragment 中使用 RecyclerView 时出现 Kotlin 错误的 Android【英文标题】:Android with Kotlin error when use RecyclerView in Fragment 【发布时间】:2018-10-13 07:28:39 【问题描述】:

我说错了:

进程:com.example.yudha.kotlinauth,PID:16435 java.lang.IllegalStateException: video_recyclerview 不能为空 在 com.example.yudha.kotlinauth.fragments.VideoFragment.onCreateView(VideoFragment.kt:30)

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)


片段活动:

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
    

我的片段 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_
    android:layout_
    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_
        android:layout_ />
</LinearLayout>

回收站视图(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_
    android:layout_>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_
        android:layout_
        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_
        android:layout_
        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>

【问题讨论】:

你要加一行 import kotlinx.android.synthetic.main.fragment_name.* 【参考方案1】:

你忘了做:

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

所以代码是这样的:-

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
    

【讨论】:

如果我们使用import kotlinx.android.synthetic.main.blah.* 在这种情况下我们不需要初始化。 由于某种原因,我也使用了合成,并给了我一个 LayoutManager 错误,手动发现它对我有用【参考方案2】:

我必须解决这个问题

只需将video_recyclerview 更改为rootView.video_recyclerview,因为在片段中您首先需要知道您使用的视图在哪里。

【讨论】:

或者你可以移动到初始化到onViewCreated @RobCo 我认为这不是一个好主意,因为这会使 GUI 陷入困境。【参考方案3】:

您忘记初始化回收站视图。

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

【讨论】:

【参考方案4】:

用下面的代码更新你的代码。

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

片段活动:

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】:

当你把它添加到你的 app.grable 文件中时,你可以通过 id 直接访问你的组件

    apply plugin: 'kotlin-android-extensions'

然后在你的片段中导入它

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

现在一切都会像魅力一样发挥作用

【讨论】:

以上是关于在 Fragment 中使用 RecyclerView 时出现 Kotlin 错误的 Android的主要内容,如果未能解决你的问题,请参考以下文章

Fragment的使用方式

在 Fragment 中使用 Spinner 和 Adapter

如何在 Fragment l 中使用 WebView? [关闭]

Fragment使用

Android Fragment的使用

在一个ViewPager中同时使用v4 Fragment和native Fragment