“Kotlin.UninitializedPropertyAccessException:lateinit属性madapter has not been initialized at...”(在设置RecyclerView适配器时,lateinit属性madapter尚未在...处初始化

weylhg0b  于 2023-06-24  发布在  Kotlin
关注(0)|答案(2)|浏览(687)

抛出行错误:

recyclerView.adapter = mAdapter

代码:

class MainActivity : AppCompatActivity(), NewsItemClicked {
    private lateinit var recyclerView:RecyclerView
    private lateinit var mAdapter: NewsListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recyclerView)
        fetchData()

        val verticalLayoutManager = LinearLayoutManager(this)
             recyclerView.layoutManager = verticalLayoutManager
        recyclerView.adapter = mAdapter
    }

    private fun fetchData() {
        val url =
            "https://newsapi.org/v2/top-headlines?country=in&apiKey=*****"
        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            {
                val newsJsonArray = it.getJSONArray("articles")
                val newsArray = ArrayList<News>()
                for (i in 0 until newsJsonArray.length()) {
                    val newsJsonObject = newsJsonArray.getJSONObject(i)
                    val news = News(
                        newsJsonObject.getString("title"),
                        newsJsonObject.getString("author"),
                        newsJsonObject.getString("url"),
                        newsJsonObject.getString("urlToImage")
                    )
                    newsArray.add(news)
                }
                mAdapter.updateNews(newsArray)
            }, {

            }

        )
        MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
    }
    override fun onItemClicked(item: News) {
        val builder = CustomTabsIntent.Builder()
        val customTabsIntent = builder.build()
        customTabsIntent.launchUrl(this, Uri.parse(item.url))
    }
}

来自logcat的错误:

2021-01-19 18:38:40.819 3869-3869/com.example.newsfresh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.newsfresh, PID: 3869
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsfresh/com.example.newsfresh.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property madapter has not been initialized
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property madapter has not been initialized
        at com.example.newsfresh.MainActivity.onCreate(MainActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:7088)
        at android.app.Activity.performCreate(Activity.java:7079)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6651) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 
deyfvvtc

deyfvvtc1#

您尚未初始化lateinit var mAdapter变量。
Kotlin允许您访问它,尽管它未初始化,因为它是lateinit,但它会抛出这个异常。你可以用两种方法来解决这个问题:

删除lateinit,在声明点初始化:

在这种情况下,它可以是val

private val mAdapter = NewsListAdapter(...)

这假设在启动Activity时您已经具备了设置适配器所需的一切。一旦数据可用,您可能需要调用notify...方法之一。
如果你不…

使用前请确认已初始化

mAdapter = NewsListAdapter(...)
recyclerView.adapter = mAdapter
p1tboqfb

p1tboqfb2#

或检查之前:

if (this::recyclerView.isInitialized) {
    ...
}

相关问题