gpt4 book ai didi

java - 使用 PDF 查看器库时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 19:02:10 25 4
gpt4 key购买 nike

我在使用 AndroidPdfViewer 库时遇到以下错误。这是我遇到的错误:

FATAL EXCEPTION: main
Process: app.com.application, PID: 16559
java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromStream(java.io.InputStream)' on a null object reference
at app.com.application.activity.Main2Activity$RetrievePDFStream.onPostExecute(Main2Activity.java:65)
at app.com.application.activity.Main2Activity$RetrievePDFStream.onPostExecute(Main2Activity.java:38)

我的代码:

public class Main2Activity extends Activity {

String URL_PDF = "http://192.168.1.103/android_login_api/PDF/1G.pdf";
PDFView pdfView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

// view pdf from url
new RetrievePDFStream().execute(URL_PDF);

}

private class RetrievePDFStream extends AsyncTask <String, Void, InputStream>{
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
URL url = null;
try {
url = new URL(strings[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}

try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode()==200){

inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
}
} catch (IOException e) {
return null;
}

return inputStream;
}


@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}

}

请指导我解决问题,谢谢。我还有另一个问题:是否可以使用此方法 Volley 而不是 AsyncTask

最佳答案

发生这种情况是因为 pdfView 为空。

关于java - 使用 PDF 查看器库时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48607724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com