[英]Kotlin recyclerview gave me null pointer exception
i started learing Kotlin after using Java, curently trying to create recyclerview using Kotlin, but everytime reach this code:我在使用 Java 后开始学习 Kotlin,目前尝试使用 Kotlin 创建 recyclerview,但每次都达到以下代码:
recyclerview!!.layoutManager = LinearlayoutManager(context)
inside a fragment, it always returns null for me, and by converting existing Java Code using converter in android studio returns error.Can anyone help me why this is always happened?在片段内,它总是为我返回 null,并且通过在 android studio 中使用转换器转换现有的 Java 代码返回错误。谁能帮助我为什么总是发生这种情况? code below is my current fragment code:
下面的代码是我当前的片段代码:
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
}
}
Thats because the views are not created yet in onCreateView
.那是因为视图尚未在
onCreateView
创建。 Since you are using kotlinx.android.synthetic
you need to access it inside onViewCreated
.Like this由于您使用的是
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)
}
Try this...试试这个...
You should declare your recyclerview object, before accessing their properties.在访问它们的属性之前,您应该声明您的 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
I just now had a similar situation.我刚才也有类似的情况。
In my project, I have a RecyclerView
working perfectly.在我的项目中,我有一个
RecyclerView
完美运行。 In my case, it has five files involved: Foo.kt
(the model), FooList.kt
(the AppCompatActivity
class), FooListAdapter.kt
, foo_list.xml
(the layout with the RecyclerView
) and foo_list_row.xml
(my custom row for the ReyclerView
).就我而言,它涉及五个文件:
Foo.kt
(模型)、 FooList.kt
( AppCompatActivity
类)、 FooListAdapter.kt
、 foo_list.xml
(带有RecyclerView
的布局)和foo_list_row.xml
(我的自定义行ReyclerView
)。 Not sure if it's important or not, but the icon for Foo.kt
is不确定它是否重要,但
Foo.kt
的图标是 while the icon for the
FooList.kt
and FooListAdapter.kt
is而
FooList.kt
和FooListAdapter.kt
的图标是
I needed another RecyclerView, so I simply created five new files: Bar.kt
, BarList.kit
, BarListAdapter.kit
, bar_list.xml
and bar_list_row.xml
.我需要另一个RecyclerView,所以我干脆创建了五个新的文件:
Bar.kt
, BarList.kit
, BarListAdapter.kit
, bar_list.xml
和bar_list_row.xml
。 I copied/pasted the code from one to the other, and made the appropriate Foo->Bar changes in the code.我将代码从一个复制/粘贴到另一个,并在代码中进行了适当的 Foo->Bar 更改。 This is when I got the same error as the OP.
这是当我遇到与 OP 相同的错误时。 Looking in the
import
section, the import for RecyclerView
was flagged as "superfluous".查看
import
部分, RecyclerView
的导入被标记为“多余”。 Odd.奇怪。
Most of the suggested answers here is to declare the variable and use findViewById
to reference it.这里的大部分建议答案是声明变量并使用
findViewById
来引用它。 But, my working RecyclerView
doesn't do that, so why should it be necessary in the second RecyclerView
?但是,我的工作
RecyclerView
并没有这样做,那么为什么在第二个RecyclerView
有必要呢? It doesn't make sense.这没有意义。
I continued to search and found this solution:我继续搜索并找到了这个解决方案:
BarList.kit
(the AppCompatActivity file) and the 'corresponding' bar_list.xml
)BarList.kit
(AppCompatActivity 文件)和“对应” bar_list.xml
)New > Activity > Empty Activity
.New > Activity > Empty Activity
。 This re-created the files I had just deleted with exactly the same names. Now both my RecyclerView
s work correctly.现在我的
RecyclerView
可以正常工作。
I guess when you use the New > Activity > Empty Activity
, Android Studio does some work in the background which is not done if you just create the files manually.我猜当您使用
New > Activity > Empty Activity
,Android Studio 会在后台执行一些工作,如果您只是手动创建文件,则不会完成这些工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.