0

I'm working on a document sharing app & when I use 'search' functionality, the app crashes I think there's some issue in PDFViewer(https://i.stack.imgur.com/qVaEF.png) PDFViewer works on dashboard but crashes when I search for an item (a document) from the dashboard Error:

---------------------------- PROCESS STARTED (3025) for package com.example.edventure ----------------------------
2023-03-27 19:51:23.789  3025-3025  AndroidRuntime          com.example.edventure                E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.edventure, PID: 3025
                                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.HandlerThread.isAlive()' on a null object reference
                                                                                                        at com.github.barteksc.pdfviewer.PDFView.loadComplete(PDFView.java:756)
                                                                                                        at com.github.barteksc.pdfviewer.DecodingAsyncTask.onPostExecute(DecodingAsyncTask.java:80)
                                                                                                        at com.github.barteksc.pdfviewer.DecodingAsyncTask.onPostExecute(DecodingAsyncTask.java:27)
                                                                                                        at android.os.AsyncTask.finish(AsyncTask.java:771)
                                                                                                        at android.os.AsyncTask.access$900(AsyncTask.java:199)
                                                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:263)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8296)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)

Possible place of error:

 public static void loadPdfFromUrlSinglePage(String pdfUrl, String pdfTitle, PDFView pdfView, ProgressBar progressBar) {
        String TAG = "PDF_LOAD_SINGLE_TAG";
        StorageReference ref = FirebaseStorage.getInstance().getReferenceFromUrl(pdfUrl);
        ref.getBytes(MAX_BYTES_PDF)
                .addOnSuccessListener(new OnSuccessListener<byte[]>() {
                    @Override
                    public void onSuccess(byte[] bytes) {
                        Log.d(TAG,"onSuccess: " + pdfTitle + "successfully got the file");

                        //set to pdfview
                        pdfView.fromBytes(bytes)
                                .pages(0)
                                .spacing(0)
                                .swipeHorizontal(false)
                                .enableSwipe(false)
                                .onError(new OnErrorListener() {
                                    @Override
                                    public void onError(Throwable t) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"onError: "+t.getMessage());
                                    }
                                })
                                .onPageError(new OnPageErrorListener() {
                                    @Override
                                    public void onPageError(int page, Throwable t) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"onPageError: "+t.getMessage());
                                    }
                                })
                                .onLoad(new OnLoadCompleteListener() {
                                    @Override
                                    public void loadComplete(int nbPages) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"loadComplete: pdf loaded");
                                    }
                                })
                                .load();
                    }
                })

I'm stuck at this error since a long time & I can't figure it out... If anything else is required pls let me know!

I checked the issues posted on github https://github.com/barteksc/AndroidPdfViewer/pull/917 but the issue isn't solved yet!

1 Answers1

0

I have just faced the same error and it came up it was indeed the bug in the PdfViewer library.

In my case the exception was raised while reloading the assosiated pdf in TabsLayout (using ViewPager2 for rendering PDFView) while moving from first tab to the other and then back to the first one (which was displaying the pdf).

The cause of exception was quite simple. The PDFView class retains its state while moving between tabs but its onDetachedFromWindow() method sets its renderingHandlerThread field of type HandlerThread to null. After that when the PDFView was loaded again its loadComplete() method was referring to the renderingHandlerThread that was null..

if (!renderingHandlerThread.isAlive()) { ..

..and that's it.

The solution was to conditionally recreate the renderingHandlerThread in case of it's null before performing the mentioned check. I have downloaded the library and add it to my project locally but I am going to make a pull request to update the github repository as well.

hlavli
  • 56
  • 3