我在android中集成html。我创建了一个Web视图,但我无法加载本地html页面。令人惊讶的是,网页视图正在加载'http:google.com',而不是我的本地html文件。我已经尝试了几乎所有可能的SO链接。错误消息是“无法加载网页”
WebView view = (WebView) findViewById(R.id.webView1);
view.loadUrl("file:///assets/www/trialhtml.html");![enter image description here][1]
答案 0 :(得分:6)
创建你的html并将它放到assets文件夹,然后你需要这样的
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/test.html");
setContentView(webView);
}
这是结果
答案 1 :(得分:0)
尝试以下代码: -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
WebView view = (WebView) findViewById(R.id.webView1);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient());
view.loadUrl(myURL);
}
检查您的文件是否存在,您也可以清理项目并重建它。
答案 2 :(得分:0)
尝试使用此代码:
this.webview = (WebView)findViewById(R.string.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
答案 3 :(得分:0)
将.html文件粘贴到项目文件夹的assets文件夹中。并在布局文件夹中使用fol代码
创建一个xml文件WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
在活动中添加fol代码
setContentView(R.layout.my);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/www/trialhtml.html"); //new.html is html file na