voghDev / PdfViewPager

Android widget that can render PDF documents stored on SD card, linked as assets, or downloaded from a remote URL.
Apache License 2.0
1.69k stars 330 forks source link

Crash On Oreo 8.1 when i try to close the PDF Adapter. #114

Open zohaib319 opened 5 years ago

zohaib319 commented 5 years ago

Crash Report java.lang.RuntimeException: at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5046) at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:5065) at android.app.ActivityThread.-wrap6 (Unknown Source) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1955) at android.os.Handler.dispatchMessage (Handler.java:108) at android.os.Looper.loop (Looper.java:166) at android.app.ActivityThread.main (ActivityThread.java:7425) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921) Caused by: java.lang.NullPointerException: at com.govpk.citizensportal.PdfManual.onDestroy (PdfManual.java:169) at android.app.Activity.performDestroy (Activity.java:7604) at android.app.Instrumentation.callActivityOnDestroy (Instrumentation.java:1253) at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:5033)

Devices which are observing crash issues.

Mate 10 lite (HWRNE) 81 21.5%
Y7 (HWTRT-Q) 23 6.1%
nova 3i (HWINE) 23 6.1%
HUAWEI Y7 Prime 2018 (HWLDN-Q) 22 5.8%
P20 lite (HWANE) 18 4.8%
HUAWEI Y9 2019 (HWJKM-H) 17 4.5%
荣耀畅玩 6X (HWBLN-H) 16 4.2%
Redmi Note 4 (mido) 11 2.9%
Honor 7C (HWLND-Q) 10 2.7%
P10 lite (HWWAS-H) 10 2.7%
荣耀 8X (HWJSN-H) 8 2.1%
HUAWEI Y9 2018 (HWFLA-H) 8 2.1%
CPH1801 (CPH1801) 7 1.9%
P8 lite 2017 (HWPRA-H) 7 1.9%
Moto E4 (perry_f) 7 1.9%
Honor 9 Lite (HWLLD-H) 5 1.3%
HUAWEI nova 2 Plus (HWBAC) 5 1.3%
Honor 7X (HWBND-H) 5 1.3%
DROID Turbo 2 (kinzie) 4 1.1%
P9 (HWEVA) 4 1.1%
Others 86 22.8%

Java code Pasted Below . import android.Manifest; import android.app.Dialog; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.os.Environment; import android.os.Handler; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewStub; import android.view.Window; import android.webkit.WebView; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast;

import com.govpk.views.FontTextView; import com.govpk.views.InfoDialogFragment; import com.gun0912.tedpermission.PermissionListener; import com.gun0912.tedpermission.TedPermission; import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; import es.voghdev.pdfviewpager.library.RemotePDFViewPager; import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter; import es.voghdev.pdfviewpager.library.remote.DownloadFile; import es.voghdev.pdfviewpager.library.util.FileUtil;

public class PdfManual extends AppCompatActivity implements DownloadFile.Listener { public static String url; @BindView(R.id.remote_pdf) RemotePDFViewPager remotePdf; public PDFPagerAdapter pdfPagerAdapter; @BindView(R.id.root_layout) RelativeLayout rootLayout; @BindView(R.id.layout_progress) LinearLayout layoutProgress; @BindView(R.id.layout_error) LinearLayout layoutError;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdf_manual);
    ButterKnife.bind(this);

// setToolbar(true, false, "Citizen Manual"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { TedPermission.with(PdfManual.this) .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE) .setDeniedMessage("If you reject permission, You will not be able to see the Manual") .setPermissionListener(new PermissionListener() { @Override public void onPermissionGranted() { showSelectionDialogue(PdfManual.this); } @Override public void onPermissionDenied(ArrayList deniedPermissions) { InfoDialogFragment infoDialogFragment = InfoDialogFragment.newInstance("Permission", "Enable storage permission"); infoDialogFragment.setOnInfoClickListener(new InfoDialogFragment.InfoDialogListener() { @Override public void onInfoDialogOkClick() {

                            }
                        });
                        infoDialogFragment.show(getSupportFragmentManager(), "permission");

                    }
                }).check();
    } else {
        showSelectionDialogue(PdfManual.this);
    }

    if (ActivityCompat.checkSelfPermission(PdfManual.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && (ActivityCompat.checkSelfPermission(PdfManual.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
        Toast.makeText(this, "Enable Storage Permission", Toast.LENGTH_SHORT).show();
    } else {

    }
}

public void showSelectionDialogue(Context context) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.manual_selection_dialogue);
    FontTextView englishSelection = dialog.findViewById(R.id.selection_english);
    FontTextView urduSelection = dialog.findViewById(R.id.selection_urdu);
    FontTextView closeDialogue = dialog.findViewById(R.id.close);
    englishSelection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            layoutProgress.setVisibility(View.VISIBLE);
            loadPDFFile("http://pmdu.pmo.gov.pk/guideline/manual/PCP-user-manual-english.pdf?fbclid=IwAR1LCkXpKoXTfEVg3kBhLHEpObKNPsY7BgD-W72hY8uRQ3ySSD-gR02L9rg");
            dialog.dismiss();

        }

    });
    urduSelection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            layoutProgress.setVisibility(View.VISIBLE);
            loadPDFFile("http://pmdu.pmo.gov.pk/guideline/manual/PCP-user-manual-urdu.pdf?fbclid=IwAR28723xxNwHglTh9ijhyKfC0FFBjS7N1CFGUDxq-_RdgRWuRpnUteQ1jzA");
            dialog.dismiss();
        }
    });
    closeDialogue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            finish();
        }
    });

    dialog.show();
}
private void loadPDFFile(String fileUrl)
{
    remotePdf =
            new RemotePDFViewPager(PdfManual.this, fileUrl, this);

}

@Override
public void onSuccess(String url, String destinationPath) {
    layoutProgress.setVisibility(View.GONE);
    pdfPagerAdapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
    remotePdf.setVisibility(View.VISIBLE);
    remotePdf.setAdapter(pdfPagerAdapter);
    setContentView(remotePdf);

}

@Override
public void onFailure(Exception e) {
    remotePdf.setVisibility(View.GONE);
    layoutProgress.setVisibility(View.GONE);
    layoutError.setVisibility(View.VISIBLE);

}

@Override
public void onProgressUpdate(int progress, int total) {
    Log.d("progress_pdf",Integer.toString(progress));
}

@Override
protected void onDestroy() {
    super.onDestroy();
    pdfPagerAdapter.close();

}

}

Anything needed I will be more then happy to provide. please resolve this issue.

voghDev commented 5 years ago

Hello,

thanks for the detailed info and the code sample. Finding the cause with this information is much easier.

Note that you are closing the adapter in onDestroy() and only assigning it in the onSuccess case, so if you close the activity without reaching onSuccess, you might get a NPE. But this doesn't seem to be your case because you are getting a RuntimeException.

I'll try to reproduce my code samples in one of the mentioned phones to see if I also get the crash in Oreo

zohaib319 commented 5 years ago

Hi Thanks for the quick reply by the way. I have this scenario working. I have a different Activity for PDF View. Adapter should be created when i navigate to that activity. some times on fresh install I am getting NPE before even navigating to the PDF Activity. This issue is very much concerning right now.

zohaib319 commented 5 years ago

And by the way i have also tried creating the new pdfpageradapter in my Custom Dialogue and this way it didn't work but prevent the crashes.