没有找到任何合适的解决方案。所以,我最终遵循了MoshErsan的建议,如此处所述:
这就是我所做的:
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
@TargetApi(11)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Log.d("shouldInterceptRequest", url);
InputStream stream = inputStreamForAndroidResource(url);
if (stream != null) {
return new WebResourceResponse("text/javascript", "utf-8", stream);
}
return super.shouldInterceptRequest(view, url);
}
private InputStream inputStreamForAndroidResource(String url) {
final String ANDROID_ASSET = "file:///android_asset/";
if (url.contains(ANDROID_ASSET)) {
url = url.replaceFirst(ANDROID_ASSET, "");
try {
AssetManager assets = getAssets();
Uri uri = Uri.parse(url);
return assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/code_snippets/sample_java.html");
不知道真正的问题是什么。但是,这个 hack 有效。