提问人:abhilash kaveriappa 提问时间:1/19/2022 更新时间:1/19/2022 访问量:512
从 Webview 调用 New Activity Intent 时应用崩溃?
App Crashing when calling New Activity Intent from Webview?
问:
我正在开发应用程序以将 PDF 文件从 Webview 加载到新的 PDF 活动中 使用意图和PdfViewer.jar库。我知道无法在Webview中打开PDF文件,但尝试在activity_pdf.xml中打开当我在Webview中单击链接时,我的应用程序无法调用PdfViewer.class和崩溃,可能的原因是什么。请帮帮我被困在这里。 代码看起来没问题,没有错误。
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);
}
参考我的 Logcat 错误
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)
这一行可能有什么错误?我在这一行上收到 logcat 错误。请帮忙
pdfurl = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
我正在使用 PdfViewer.jar 库,在 README 文本中提到了使用,请参阅下面的链接供您参考,https://sourceforge.net/p/andpdf/code/HEAD/tree/tag/Beta_0_1_11/AndroidPdfViewer/activitysrc/net/sf/andpdf/
答:
从获取 uri 的位置。
它清楚地表明 uri 为 null 意味着没有值。
如果您从以前的活动中获得它,请使用它。
final Uri uri= getIntent().getStringExtra("name");
问题是你的 uri ()
变量不会从 intent 中获取数据。因此,您需要首先获取该用途的数据,如@Manjeetdeswal回答的那样Uri uri = PdfActivity.this.getIntent().getData();
final Uri uri= getIntent().getStringExtra("name");
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);
}
检查 URL 中是否包含 pdf,然后将 URL 传递给另一个活动
只需将起始意图代码移动到 try catch 中即可。仅当 pdfurl 存在时,才会启动新活动或显示错误。
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(意向); System.out.println(“PDF 工作”); } catch (MalformedURLException e) { e.printStackTrace(); }
return super.shouldOverrideUrlLoading(view, url);
}
评论