Androidpdfviewer: java.lang.IllegalStateException: Don't call load on a PDF View without recycling it first

Created on 6 Dec 2017  路  17Comments  路  Source: barteksc/AndroidPdfViewer

I have a tab view wherein three tabs exists. All the tab has pdfview. Each tab loads different pdf file from the database. Whenever the downloaded file loads in the pdfview (all this is happening inside async task) the following error shows up and then the app crashes

java.lang.IllegalStateException: Don't call load on a PDF View without recycling it first

I am not sure what am I supposed to do!

class MyAsync extends AsyncTask<Void, Void, Void> {


           PDFView pdfView;


            public MyAsync(PDFView pdfView) {

               this.pdfView = pdfView;


            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pd.setMessage("Uploading . . .");
                pd.show();
                pd.setCancelable(false);
            }

            @Override
            protected Void doInBackground(Void... voids) {

               publishProgress();

                return null;
            }

            @Override
            protected void onProgressUpdate(Void... values) {




                dataRef.child(dbs).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChild(root_child)) {
                            String url = dataSnapshot.child(root_child).getValue().toString();
                            StorageReference island = storage.getReferenceFromUrl(url);

                            final File file;

                            try {
                                file = File.createTempFile(file_name, "pdf");
                                island.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                                    @Override
                                    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                                        yes_file(pdfView, tv1, tv2);
                                        pdfView.fromFile(file).load();

                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(getActivity(), "Upload unsuccessful", Toast.LENGTH_LONG).show();
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                            }


                        } 
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                pd.dismiss();
            }
        }

Most helpful comment

Issue should be fixed in 3.0.0-beta.5 version

All 17 comments

I added the line pdfView.recycle(); before pdfView.fromFile(file).load(); But still the problem persists.Not sure if this the right way to recycle! Pls help. I Am stuck

Same here i am also getting same error "java.lang.IllegalStateException: Don't call load on a PDF View without recycling it first" and unable to resolve can you please help

Is pdfView.fromFile(file).load() called in UI thread?

mPdfView.fromUri(pdfURI)
.swipeHorizontal(false)
.enableDoubletap(true)
.load();

this is what i have called from main thread

You are calling pdfView.fromFile(file).load() when switching tabs, are this calls executed from main thread?

I am Using in Gridview and calling this from my main thread
mPdfView.fromUri(pdfURI)
.swipeHorizontal(false)
.enableDoubletap(true)
.load();

yes I Am calling pdfView.fromFile(file).load() in ui thread @barteksc

Having the same problem in a RecicleView. When I scroll I have the same problem.
Im executing the next line in the Main Thread: pdfView.fromBytes(bytes).load();

Having the same problem (Huawei device)

I can't reproduce it myself, but I have a few crashes because of that error every day. It was fine before I upgraded to v2.8.0. I think the bug was introduced in commit 499ffbc77f5f031d4166c1c544b28aeeb7b7cc94. In load() method there was added PDFView.this.post() call.

@barteksc do you remember why it was added?

@MaciejCzekanski
I cannot reproduce this issue, please show your code.

.post() was added to wait for full UI initialization, because sometimes PDF document was loaded faster than PDFView was added to layout and view was empty.

@barteksc
PDF file is fetched from backend and when it finishes that code is called from the main thread:

    pdfView.fromFile(pdfFile)
        .spacing(2)
        .onRender({ pages, pageWidth, pageHeight ->
            pdfView?.let {
                pdfView.isEnabled = true
                pdfView.fitToWidth()
                pdfView.setBackgroundColor(ContextCompat.getColor(context, R.color.silver_sand))
            }
        })
        .load()

I think it is somehow possible to call pdfView.load() twice in a way that the second call gets "recycled = false" value.

Issue should be fixed in 3.0.0-beta.5 version

@barteksc when are you planing to release 3.0.0-beta.5 version?

It was released few days ago

Scroll big pdf,carsh!!!Help.......

It was released few days ago

Scroll big pdf,carsh!!!Help.......Big cow......

Was this page helpful?
0 / 5 - 0 ratings