AndroidPdfViewer在KitKat版本中不起作用



我正在将此库用于AndroidPdfViewerhttps://github.com/barteksc/AndroidPdfViewer

E/PDFView: load pdf error
java.lang.NullPointerException
at com.github.barteksc.pdfviewer.util.Util.toByteArray(Util.java:36)
at com.github.barteksc.pdfviewer.source.InputStreamSource.createDocument(InputStreamSource.java:37)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

我在这一行得到了nullPointer异常。

inputStream=new BufferedInputStream(urlConnection.getInputStream());

这似乎不是库错误,因为这是由于null参数引发的NullPointer。您的PDFViwer无法打开该文件,因此它抛出了空指针异常。你可以检查你的代码的原因。你应该遵循最佳实践来防止这种情况的发生。请先检查连接,它不应为null。您可以使用?三元运算符进行检查,如下所示。

// InputStream in = conn.getInputStream();
// check for the null connection first, it's possible that connection could not be made before opening the InputStream.
InputStream in = (conn != null) ? conn.getInputStream() : null;

最新更新