尝试在空对象引用上调用接口方法'java.lang.Object [] java.util.Collection.toArray()'(仅在发行版中)

问题描述 投票:0回答:1

我在项目中使用kotlin。我仅在发行版上收到此错误。在调试版本中,一切正常。它发生在此行postList.addAll(response.body()!!.data)

而且我以前像val postList: MutableList<Post> = mutableListOf()那样初始化它>

知道为什么会这样吗?

这里是完整代码

private fun initRecyclerView(){
    adapter = PostsAdapter(context!!, postList)
    val linearLayoutManager = LinearLayoutManager(context)
    rvPosts.layoutManager = linearLayoutManager
    rvPosts.adapter = adapter
}

private fun getPosts(){

    swipToRefresh.isRefreshing = true

    disposable.add(
        apiService.getPostsFeed(
            Config.token)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object: DisposableSingleObserver<Response<PostData>>() {
                override fun onSuccess(response: Response<PostData>) {
                    swipToRefresh.isRefreshing = false

                    when {
                        response.isSuccessful -> {
                            postList.clear()
                            if(response.body() != null){
                                postList.addAll(response.body()!!.data)
                                adapter.notifyDataSetChanged()
                                noPostsLayout.visibility = View.GONE
                            }
                        }
                        response.code() == 404 -> {
                            noPostsLayout.visibility = View.VISIBLE
                        }
                        else -> {
                            Toast.makeText(
                                context,
                                getString(R.string.error_general_failure),
                                Toast.LENGTH_LONG
                            ).show()
                            noPostsLayout.visibility = View.GONE
                        }
                    }
                }

                override fun onError(e: Throwable) {
                    Timber.d("onError ${e.message}")
                    swipToRefresh.isRefreshing = false
                    if(!Config.isNetworkConnected(context!!)){
                        Toast.makeText(context, getString(R.string.error_internet_connection), Toast.LENGTH_LONG).show()
                    }else{
                        Toast.makeText(context, getString(R.string.error_server_connection), Toast.LENGTH_LONG).show()
                    }
                }

            })
    )

}

我在项目中使用kotlin。我仅在发行版上收到此错误。在调试版本中,一切正常。它发生在这一行上postList.addAll(response.body()!!。data)...

java android kotlin arraylist apk
1个回答
0
投票

尝试postList.addAll(response.body()?.data?)postList.addAll(response.body()!!.data?)

© www.soinside.com 2019 - 2024. All rights reserved.