切换BottomNavigationView标签会导致崩溃E / AndroidRuntime:致命例外:OkHttp Dispatcher

时间:2019-01-25 02:40:21

标签: java android kotlin okhttp bottomnavigationview

我有一个BottomNavigationView,其中包含多个标签。每个选项卡都包含一个片段。选择从Web调用Json的片段并快速切换到另一个选项卡时,应用程序崩溃。

  

E / AndroidRuntime:致命例外:OkHttp Dispatcher

这是我的代码:

fun fetchJson(theRecycler: RecyclerView) {
    val request = Request.Builder().url("someUrl").build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object : Callback {
        override fun onResponse(call: Call?, response: Response?) {
            val body = response?.body()?.string()
            val gson = GsonBuilder().create()
            val myFeed = gson.fromJson(body, WebFragment::class.java)

            activity!!.runOnUiThread{
                theRecycler.adapter = WebAdapter(myFeed.items)
            }
        }
        override fun onFailure(call: Call?, e: IOException?) {
                println("Failed to execute request")
        }
    })
}

此行似乎发生了错误:

            activity!!.runOnUiThread{
                theRecycler.adapter = WebAdapter(myFeed.items)
            }

我认为我需要停止此过程onStop(),但不确定如何继续进行。

  

2019-01-24 22:25:49.390 22261-22510 / com.app E / AndroidRuntime:   致命异常:OkHttp分派器       流程:com.app,PID:22261       kotlin.KotlinNullPointerException           在com.app.UI.Navigation.HomeFeed.NewsFeed $ fetchJson $ 1.onResponse(HomeFeed.kt:45)           在okhttp3.RealCall $ AsyncCall.execute(RealCall.java:153)           在okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)           在java.lang.Thread.run(Thread.java:764)

0 个答案:

没有答案