gson 解析JSON时出现空指针异常(仅在某些情况下)

q3qa4bjr  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(250)

我正在使用这段代码来解析一个json文件,并更改我构建的de app中的一些项:

fun fetchJson() {

        if (isNetworkAvailable()) {

            val request = Request.Builder().url(stationAPPJsonURL).build()

            client.newCall(request).enqueue(object : Callback {

                override fun onResponse(call: Call, response: Response) {

                    val body = response.body?.string()

                    val gson = GsonBuilder().create()

                    try {

                        val stationAPP = gson.fromJson(body, StationAPP::class.java)

                        //ACTIE
                        actie_zichtbaar = stationAPP.actie_zichtbaar
                        actie_img = stationAPP.actie_img
                        actie_url = stationAPP.actie_url

... I left out some code

                    catch (error: JsonParseException) {

                        runOnUiThread {

                            val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)

                            val view = toast.view!!.findViewById<TextView>(android.R.id.message)
                            toast?.let { view.gravity = Gravity.CENTER }
                            toast.setGravity(Gravity.CENTER, 0, 0)
                            toast.show()

                        }

                    }

               }

                override fun onFailure(call: Call, e: IOException) {
                runOnUiThread {

                    val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)

                    val view = toast.view!!.findViewById<TextView>(android.R.id.message)
                    view?.let { view.gravity = Gravity.CENTER }
                    toast.setGravity(Gravity.CENTER, 0, 0)
                    toast.show() --> line 1390!!

                }

            }

         }

  }

我在Google Play控制台的日志中看到这部分代码有时会崩溃(4%)。我不知道为什么?我在几台设备上测试过,没有任何错误。
谁能帮我一下?
是否可能:瓦尔stationAPP = gson.fromJson(body,StationAPP::class.java)没有抛出异常,但不知何故,加载的json文件中有一些错误?因此,有些项为空?
谢谢你的帮助;)
更新:这是它产生的错误:

Exception java.lang.NullPointerException:
  at com.familiekoning.grolloo.MainActivity$fetchJson$1.onFailure$lambda-10 (MainActivity.kt:1390)
  at com.familiekoning.grolloo.MainActivity$fetchJson$1.$r8$lambda$lczurdfMtnZdFDn9iwHvPqw9_Pg
  at com.familiekoning.grolloo.MainActivity$fetchJson$1$$ExternalSyntheticLambda8.run
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:250)
  at android.app.ActivityThread.main (ActivityThread.java:7806)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
xhv8bpkk

xhv8bpkk1#

这将是一个猜测,但如果你看到!!在任何地方,你有NullPointerException。你是非常接近肯定,它是罪魁祸首。如果你想一个不同的吐司外观有NPE安全ways
依赖于android.id.x的东西是不安全的,因为它可能非常依赖OEM。

zpf6vheq

zpf6vheq2#

我改口:

val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)

            val view = toast.view!!.findViewById<TextView>(android.R.id.message)
            view?.let { view.gravity = Gravity.CENTER }
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()

至:

val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)

            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()

这就解决了问题。

相关问题