使用PDF Viewer Lib时出现空指针异常

时间:2018-02-04 11:49:45

标签: java android android-asynctask nullpointerexception android-volley

使用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

1 个答案:

答案 0 :(得分:2)

这是因为pdfView为空。