AndroidPdfViewer icon indicating copy to clipboard operation
AndroidPdfViewer copied to clipboard

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

Open Maddy14 opened this issue 8 years ago • 24 comments

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();
            }
        }

Maddy14 avatar Dec 06 '17 16:12 Maddy14

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

Maddy14 avatar Dec 07 '17 04:12 Maddy14

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

aayushiGWL avatar Dec 07 '17 11:12 aayushiGWL

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

barteksc avatar Dec 07 '17 12:12 barteksc

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

this is what i have called from main thread

aayushiGWL avatar Dec 07 '17 12:12 aayushiGWL

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

barteksc avatar Dec 07 '17 13:12 barteksc

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

aayushiGWL avatar Dec 07 '17 13:12 aayushiGWL

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

Maddy14 avatar Dec 07 '17 13:12 Maddy14

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();

gustavohansen avatar Dec 18 '17 20:12 gustavohansen

Having the same problem (Huawei device)

carbonaro avatar Dec 28 '17 10:12 carbonaro

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 avatar Jan 05 '18 14:01 MaciejCzekanski

@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 avatar Jan 05 '18 14:01 barteksc

@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.

MaciejCzekanski avatar Jan 05 '18 14:01 MaciejCzekanski

Issue should be fixed in 3.0.0-beta.5 version

barteksc avatar Jan 06 '18 16:01 barteksc

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

gustavohansen avatar Jan 11 '18 17:01 gustavohansen

It was released few days ago

barteksc avatar Jan 11 '18 17:01 barteksc

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

farPlace avatar Aug 12 '19 02:08 farPlace

It was released few days ago

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

farPlace avatar Aug 12 '19 03:08 farPlace

I have try with lot's of way. But still this error is there. Can you please help on this issue.

kcs-hardikparmar avatar Apr 21 '22 04:04 kcs-hardikparmar

谢谢    祝你天天开心哈

farPlace avatar Apr 21 '22 05:04 farPlace

Yes,what can i do for you?

发自我的iPhone

------------------ Original ------------------ From: kcs-hardikparmar @.> Date: Thu,Apr 21,2022 1:00 PM To: barteksc/AndroidPdfViewer @.> Cc: farPlace @.>, Comment @.> Subject: Re: [barteksc/AndroidPdfViewer] java.lang.IllegalStateException:Don't call load on a PDF View without recycling it first (#464)

I have try with lot's of way. But still this error is there. Can you please help on this issue.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

farPlace avatar Apr 21 '22 05:04 farPlace