[英]Kotlin recyclerview gave me null pointer exception
我在使用 Java 后开始学习 Kotlin,目前尝试使用 Kotlin 创建 recyclerview,但每次都达到以下代码:
recyclerview!!.layoutManager = LinearlayoutManager(context)
在片段内,它总是为我返回 null,并且通过在 android studio 中使用转换器转换现有的 Java 代码返回错误。谁能帮助我为什么总是发生这种情况? 下面的代码是我当前的片段代码:
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_notes.*
import java.sql.Timestamp
class NotesFragment : Fragment() {
companion object {
fun newInstance(): NotesFragment {
return NotesFragment()
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.fragment_notes, container, false)
val messageList : MutableList<message> = prepareMessage()
recycler_view!!.layoutManager = LinearLayoutManager(context)
recycler_view!!.adapter = NotesAdapter(context)
(recycler_view!!.adapter as NotesAdapter).setMessage(messageList)
return view
}
private fun prepareMessage(): MutableList<message> {
var messageList : MutableList<message> = ArrayList()
for(i in 0..10){
val timestamp: Long = System.currentTimeMillis()/1000L
val message = message(0,
"Judul Catatan ke $i",
resources.getString(R.string.lipsum),
timestamp
)
messageList.add(message)
}
return messageList
}
}
那是因为视图尚未在onCreateView
创建。 由于您使用的是kotlinx.android.synthetic
您需要在onViewCreated
访问它。像这样
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.fragment_notes, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
recycler_view = view.findViewById(R.id.your_recycler_id)
recycler_view?.layoutManager = LinearLayoutManager(context)
recycler_view?.adapter = NotesAdapter(context)
val messageList : MutableList<message> = prepareMessage()
(recycler_view?.adapter as NotesAdapter).setMessage(messageList)
}
试试这个...
在访问它们的属性之前,您应该声明您的 recyclerview 对象。
.....
private var recycler_view: RecyclerView? = null
.....
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.fragment_notes, container, false)
recycler_view = view.findViewById(R.id.your_recycler_id)
recycler_view?.layoutManager = LinearLayoutManager(context)
recycler_view?.adapter = NotesAdapter(context)
val messageList : MutableList<message> = prepareMessage()
(recycler_view?.adapter as NotesAdapter).setMessage(messageList)
return view
}
你有没有像这样声明你的 recylerview
polishrecyler = view .findViewById<View>(R.id.polish_recyler) as RecyclerView
我刚才也有类似的情况。
在我的项目中,我有一个RecyclerView
完美运行。 就我而言,它涉及五个文件: Foo.kt
(模型)、 FooList.kt
( AppCompatActivity
类)、 FooListAdapter.kt
、 foo_list.xml
(带有RecyclerView
的布局)和foo_list_row.xml
(我的自定义行ReyclerView
)。 不确定它是否重要,但Foo.kt
的图标是 而
FooList.kt
和FooListAdapter.kt
的图标是
我需要另一个RecyclerView,所以我干脆创建了五个新的文件: Bar.kt
, BarList.kit
, BarListAdapter.kit
, bar_list.xml
和bar_list_row.xml
。 我将代码从一个复制/粘贴到另一个,并在代码中进行了适当的 Foo->Bar 更改。 这是当我遇到与 OP 相同的错误时。 查看import
部分, RecyclerView
的导入被标记为“多余”。 奇怪。
这里的大部分建议答案是声明变量并使用findViewById
来引用它。 但是,我的工作RecyclerView
并没有这样做,那么为什么在第二个RecyclerView
有必要呢? 这没有意义。
我继续搜索并找到了这个解决方案:
BarList.kit
(AppCompatActivity 文件)和“对应” bar_list.xml
)New > Activity > Empty Activity
。 这以完全相同的名称重新创建了我刚刚删除的文件。 现在我的RecyclerView
可以正常工作。
我猜当您使用New > Activity > Empty Activity
,Android Studio 会在后台执行一些工作,如果您只是手动创建文件,则不会完成这些工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.