指定为非空 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
这是我的代码
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()
}
}
}
inputStream 在某些情况下为 null。当该 null 值传递给 onPostExecute 时,Kotlin 内在方法 barfs 因为您已将参数指定为非 null。
Tl;dr:将 onPostExecute 更改为具有可为 null 的参数。
您在 doInBackground()
方法中 return 可以为空。
所以,onPostExecute()
的参数应该是可空类型。
即 onPostExecute(inputStream: InputStream)
应该是 onPostExecute(inputStream: InputStream?)
。
我有java代码,我改成kotlin,我的代码是用pdf-viewer库来显示pdf的,我不明白为什么我的代码出错,出现以下错误:
The specified as non-null is null parameter: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, inputStream parameters
这是我的代码
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()
}
}
}
inputStream 在某些情况下为 null。当该 null 值传递给 onPostExecute 时,Kotlin 内在方法 barfs 因为您已将参数指定为非 null。
Tl;dr:将 onPostExecute 更改为具有可为 null 的参数。
您在 doInBackground()
方法中 return 可以为空。
所以,onPostExecute()
的参数应该是可空类型。
即 onPostExecute(inputStream: InputStream)
应该是 onPostExecute(inputStream: InputStream?)
。