【问题标题】:How to resolve "recyclerView must not be null"如何解决“recyclerView 不能为空”
【发布时间】:2020-03-28 16:16:47
【问题描述】:

我正在尝试在使用 Retrofit 解析数据后在 Activity 中实现 Recycler 视图。但问题是它显示 Recycler 视图即使在之前在 onCreate 方法内部初始化之后也不能为空访问。

Mainactitivty.kt

        class MainActivity : AppCompatActivity() {
        lateinit var myRecyclerView: RecyclerView

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        retroInstance = RetroInstance()
        val instance = retroInstance.getInstance()
        val api = instance.create(RetroInterface::class.java)
        val callAll=api.getAllDetail()
    callAll.enqueue(object :retrofit2.Callback<ModelAll>{
           override fun onFailure(call: Call<ModelAll>, t: Throwable) {
               Toast.makeText(applicationContext,t.message,Toast.LENGTH_LONG).show()
           }

           override fun onResponse(call: Call<ModelAll>, response: Response<ModelAll>) {
               val allDetail=response.body()!!
                myRecyclerView=findViewById(R.id.recyclerView)
              myRecyclerView.layoutManager=LinearLayoutManager(this@MainActivity)
               myRecyclerView.adapter=CoronaAdapter(allDetail)
           }

       })
    }
  }

包含回收站视图的活动

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".AllCountries">


    <SearchView
        android:id="@+id/searchView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="2dp"
        android:background="@drawable/custom_search"
        android:elevation="5dp"
        android:queryHint="Search here..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>

Logcat https://gist.github.com/devpawann/af3cef9d204a6f99cd7ed11937684fa2

编辑:- 问题已解决,错误是我在另一个活动类中初始化回收站视图

【问题讨论】:

  • 你能给我们看看你的activity_main.xml吗?
  • @ConfusedPup 我已经更新了。
  • 这确实很奇怪你确定你正在导入正确的recyclerView吗?尝试在 OnResponse 之外调用它或检查 seachView 是否为空
  • @ConfusedPup 是的,它在 OnResponse 之外也显示相同
  • 你能把你的错误logcat放上去吗

标签: android android-studio kotlin android-recyclerview


【解决方案1】:

你可以试试这样的:

class MainActivity : AppCompatActivity() {
        private var allDetails: MutableList<ModelAll> = ArrayList()

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        retroInstance = RetroInstance()
        val instance = retroInstance.getInstance()
        val api = instance.create(RetroInterface::class.java)
        val callAll=api.getAllDetail()


        //Init your recyclerview
        val myRecyclerView = findViewById(R.id.recyclerView)
        myRecyclerView.layoutManager=LinearLayoutManager(this)
        val coronaAdapter = CoronaAdapter(allDetails)
        myRecyclerView.adapter = adapter

        callAll.enqueue(object :retrofit2.Callback<ModelAll>{
           override fun onFailure(call: Call<ModelAll>, t: Throwable) {
               Toast.makeText(applicationContext,t.message,Toast.LENGTH_LONG).show()
           }

           override fun onResponse(call: Call<ModelAll>, response: Response<ModelAll>) {
               if(response.isSucessful()){
                    allDetails = response.body()!!
                    coronaAdapter.notifyDataSetChanged()
               }
           }

       })
}

编辑:问题在于recyclerView 处于另一个膨胀的布局中。

【讨论】:

  • 在线val myRecyclerView = findViewById(R.id.recyclerView)仍然同样的错误
  • 您可以在您的帖子中添加 logcat 吗?不只是一行
  • gist.github.com/devpawann/af3cef9d204a6f99cd7ed11937684fa2 我无法修改问题,请参阅此处的表格
  • 你确定你在膨胀正确的布局吗?您的 recyclerview 似乎不在 activity_main
  • 是的 irecycler 视图在另一个活动中,我在 findviewbyID 中引用了同样的内容
【解决方案2】:

您的 recyclerView 尚未实例化(目前它指的是 null)。

添加这一行

 recyclerview = findViewById(R.id.recyclerView)

或者,如果您使用的是 Android 扩展程序,请确保您使用的是正确的 recyclerview

【讨论】:

  • 在这种情况下,您还没有实例化回收器视图。将您的 recyclerView 变量设为实例变量,在 oncreate 中使用 recyclerview = findViewById(R.id.recyclerView)对其进行初始化>
  • 现在显示java.lang.IllegalStateException: findViewById(R.id.recyclerView) must not be null
  • 说的一样。你的recyclerview指的是null..尝试在```recyclerView.layoutManager=LinearLayoutManager(this@MainActivity)`之前添加recyclerview = findViewById(R.id.recyclerView) `
  • 请发布您所做的新更改
  • 我已更新更改。如果这有帮助,我将这些 recyclerview 布局管理器的所有代码放在另一个名为 form onCreate 方法的函数中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多