android - 如何解决 "recyclerView must not be null"

标签 android android-studio kotlin android-recyclerview

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

Mainactivity.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)
           }

       })
    }
  }

包含回收器 View 的 Activity

<?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 类中初始化了回收器 View

最佳答案

你也许可以尝试这样的事情:

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 位于另一种布局中,该布局已膨胀。

关于android - 如何解决 "recyclerView must not be null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903360/

相关文章:

android - 如何在Android中获取联系人的所有详细信息

android - 构建 gradle 项目信息卡在没有状态

android - 解决方案 : Run game local (file:///) Construct 2

带条件的 Kotlin distinctBy

android - 更改 Gridview 的图像

android - 总是收到错误消息 : No drawer view found with gravity LEFT

android - 如何在 Windows Phone 8 中安装 .xap phonegap build?

android - 从 android studio 中的 gradle 路径中删除文件/jar/类

android - 由于转换为使用 Gradle Kotlin DSL,无法解决依赖关系

kotlin - Kotlin 中线程和协程的区别