我正在尝试制作一个打开本地 html 页面的应用程序,该页面具有链接到其他 2 个页面的超链接。所有这些 html 文件都放在 assets 文件夹中。现在,当我触摸第一个 html 文件的超链接时,应用程序在打开另一个 html 文件时崩溃。
package com.example.rishabh.webviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web= (WebView) findViewById(R.id.webview);
web.loadUrl("file:///android_asset/1.html");
}
}
这是日志
W/System.err: android.os.FileUriExposedException:file:///android_asset/2.html exposed beyond app through Intent.getData()
W/System.err: at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
W/System.err: at android.net.Uri.checkFileUriExposed(Uri.java:2346)
W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8965)
W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8926)
W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4225)
W/System.err: at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4183)
W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
W/System.err: at android.app.Activity.startActivity(Activity.java:4522)
W/System.err: at android.app.Activity.startActivity(Activity.java:4490)
W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
W/System.err: at org.chromium.android_webview.ResourcesContextWrapperFactory$WebViewContextWrapper.startActivity(ResourcesContextWrapperFactory.java:121)
W/System.err: at org.chromium.android_webview.AwContentsClient.sendBrowsingIntent(AwContentsClient.java:203)
W/System.err: at org.chromium.android_webview.AwContentsClient.shouldIgnoreNavigation(AwContentsClient.java:170)
W/System.err: at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:256)
W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
A/chromium: [FATAL:jni_android.cc(236)] Please include Java exception stack in crash report
W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
W/google-breakpad: Chrome build fingerprint:
W/google-breakpad: 1.0
W/google-breakpad: 1
W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 4203 (abh.webviewdemo)
[ 11-13 22:33:57.340 1186: 1186 W/ ]
debuggerd: handling request: pid=4203 uid=10073 gid=10073 tid=4203
Application terminated.
正如@CommonsWare 所建议的,似乎仅使用以下方法就可以解决 N/25 设备尝试加载基于 Assets 的链接 html 文件的问题。在运行 Nougat 的模拟器上测试:
web.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.i("WebView", "Attempting to load URL: " + url);
view.loadUrl(url);
return true;
}
});
我是一名优秀的程序员,十分优秀!