使用PDF Viewer Lib时,空指针异常



我使用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是null。

最新更新