如何使用Kotlin在Android中使用AsyncTask?

时间:2018-11-19 02:03:14

标签: android kotlin

我需要在项目中应用AsynTask,然后输入如下内容

 private class UpgradeDB(textView: TextView) : AsyncTask<String, String, String>() {
        val innerTextView: TextView? = textView}

但是我得到这个错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.eboss.mobilesales.pb, PID: 1596
                  java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter result
                      at com.eboss.mobilesales.MainDBAdminActivity$UpgradeDB.onPostExecute(Unknown Source:21)
                      at com.eboss.mobilesales.MainDBAdminActivity$UpgradeDB.onPostExecute(MainDBAdminActivity.kt:158)
                      at android.os.AsyncTask.finish(AsyncTask.java:695)
                      at android.os.AsyncTask.-wrap1(Unknown Source:0)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6940)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

错误在哪里?

1 个答案:

答案 0 :(得分:0)

为参数传递null的堆栈跟踪点。

您可以添加?为其类型:

private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() {
    val innerTextView: TextView? = textView}

已编辑: 没有完整的代码,请尝试:

 override fun onPostExecute(result: String?) {
    super.onPostExecute(result)
    // ...
}