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
From where you getting the uri.
It clearly show that uri is null means have no value.
if you are getting it from previous activity use this.
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 answeredcheck the pdf is contained in the URL then pass the URL to another activity
Just move the start intent code into the try catch. Only if pdfurl is present start the new activity or show error.
intent.putExtra("PDFURL", pdfurl);
startActivity(intent);
System.out.println("PDF WORKING");
} catch (MalformedURLException e) {
e.printStackTrace();
}