skip to Main Content

I’m developing app to load PDF file into new PDF activity from Webview
using intent & PdfViewer.jar library. I know it is not possible to open PDF files in Webview but trying to open in activity_pdf.xml
When i click link in Webview my App fails to call PdfViewer.class & Crashing, What can be the possible cause. Please help I’m stuck here.
Code looks fine no errors.

public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
            final Uri uri = PdfActivity.this.getIntent().getData();
            URL pdfurl = null;

            try {
                pdfurl = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            intent.putExtra("PDFURL", pdfurl);
            startActivity(intent);
            System.out.println("PDF WORKING");

            return super.shouldOverrideUrlLoading(view, url);
        }

Refer my Logcat error

2022-01-19 04:17:12.401 22159-22159/ak.wp.meto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ak.wp.meto, PID: 22159
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
        at ak.wp.meto.activity.PdfActivity$MyWebViewClient.shouldOverrideUrlLoading(PdfActivity.java:90)
        at android.webkit.WebViewClient.shouldOverrideUrlLoading(WebViewClient.java:83)
        at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(Unknown Source:90)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:335)
        at android.os.Looper.loop(Looper.java:206)
        at android.app.ActivityThread.main(ActivityThread.java:8595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

What can be the error in this line? I’m getting logcat error on this line. Please help

        pdfurl = new URL(uri.getScheme(), uri.getHost(), uri.getPath());

I’m using PdfViewer.jar library, In README text it is mentioned use, See below link for your reference,
https://sourceforge.net/p/andpdf/code/HEAD/tree/tag/Beta_0_1_11/AndroidPdfViewer/activitysrc/net/sf/andpdf/

4

Answers


    1. From where you getting the uri.

    2. It clearly show that uri is null means have no value.

    3. if you are getting it from previous activity use this.

        final Uri uri= getIntent().getStringExtra("name");
      
    Login or Signup to reply.
  1. the problem is that your uri (Uri uri = PdfActivity.this.getIntent().getData();)
    variable does not get data from intent. so you need to get data first for that use final Uri uri= getIntent().getStringExtra("name"); as @Manjeetdeswal answered

    Login or Signup to reply.
  2. public class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent;
    URL pdfurl = null;
    if( PdfActivity.this.getIntent().getStringExtra("your key")!=null){
                final Uri uri = PdfActivity.this.getIntent().getData();
                try {
                    pdfurl = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
    }
    if(/*check the URL is pdf contained URL*/){// check with url(String)
                intent = new Intent(PdfActivity.this,PdfViewer.class);
                intent.putExtra("PDFURL", pdfurl);
                startActivity(intent);
                System.out.println("PDF WORKING");
    }
                return super.shouldOverrideUrlLoading(view, url);
            }
    

    check the pdf is contained in the URL then pass the URL to another activity

    Login or Signup to reply.
  3. Just move the start intent code into the try catch. Only if pdfurl is present start the new activity or show error.

     public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
            final Uri uri = PdfActivity.this.getIntent().getData();
            URL pdfurl = null;
    
            try {
                pdfurl = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
    

    intent.putExtra("PDFURL", pdfurl);
    startActivity(intent);
    System.out.println("PDF WORKING");
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }

            return super.shouldOverrideUrlLoading(view, url);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search