簡體   English   中英

Kotlin recyclerview 給了我空指針異常

[英]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.ktAppCompatActivity類)、 FooListAdapter.ktfoo_list.xml (帶有RecyclerView的布局)和foo_list_row.xml (我的自定義行ReyclerView )。 不確定它是否重要,但Foo.kt的圖標是在此處輸入圖片說明 FooList.ktFooListAdapter.kt的圖標是在此處輸入圖片說明

我需要另一個RecyclerView,所以我干脆創建了五個新的文件: Bar.ktBarList.kitBarListAdapter.kitbar_list.xmlbar_list_row.xml 我將代碼從一個復制/粘貼到另一個,並在代碼中進行了適當的 Foo->Bar 更改。 這是當我遇到與 OP 相同的錯誤時。 查看import部分, RecyclerView的導入被標記為“多余”。 奇怪。

這里的大部分建議答案是聲明變量並使用findViewById來引用它。 但是,我的工作RecyclerView並沒有這樣做,那么為什么在第二個RecyclerView有必要呢? 這沒有意義。

我繼續搜索並找到了這個解決方案:

  1. 保存在我手動創建的兩個文件中找到的代碼( BarList.kit (AppCompatActivity 文件)和“對應” bar_list.xml
  2. 刪除那些文件。
  3. 右鍵單擊包名稱並選擇New > Activity > Empty Activity 這以完全相同的名稱重新創建了我剛剛刪除的文件。
  4. 將步驟 1 中保存的代碼恢復到適當的文件。

現在我的RecyclerView可以正常工作。

我猜當您使用New > Activity > Empty Activity ,Android Studio 會在后台執行一些工作,如果您只是手動創建文件,則不會完成這些工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM