关于android:参数指定为非null kotlin

Parameter specified as non-null kotlin

我有Java代码,我更改为kotlin,我的代码用于通过使用pdf-viewer库显示pdf,但我不明白为什么我的代码有错误并出现以下错误:

The specified as non-null is null parameter: method
kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, inputStream
parameters

这是我的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    package pdfviewer.pdfviewer

import android.annotation.SuppressLint
import android.app.Activity
import android.os.AsyncTask
import android.os.Bundle
import com.github.barteksc.pdfviewer.PDFView
import java.io.BufferedInputStream
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL

class PdfRender : Activity() {

lateinit var pdfView: PDFView

override fun onCreate( savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.pdf_render)
}

@SuppressLint("StaticFieldLeak")
inner class DownloadPdf : AsyncTask<String, Void, InputStream>() {

    override fun doInBackground(vararg strings: String): InputStream? {
        var inputStream: InputStream? = null
        try {
            val uri = URL(strings[0])
            val urlConnection = uri.openConnection() as HttpURLConnection
            if (urlConnection.responseCode == 200) {
                inputStream = BufferedInputStream(urlConnection.inputStream)
            }
        } catch (e: MalformedURLException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return inputStream
    }

    override fun onPostExecute(inputStream: InputStream) {
        pdfView.fromStream(inputStream).load()
    }
     }
    }

您在doInBackground()方法中返回的内容可以为空。
因此,onPostExecute()的参数应为可为空的类型。

onPostExecute(inputStream: InputStream)应该是onPostExecute(inputStream: InputStream?)


在某些情况下,inputStream为null。 当将该空值传递给onPostExecute时,由于您已将参数指定为非空,因此Kotlin内在方法会使用barfs。

Tl; dr:将onPostExecute更改为具有可为空的参数。