WebView未正确显示网站。任何帮助都会很棒! 代码已经在所有其他网站上使用了。不确定这个问题是什么。我应该添加什么?适用于Chrome和其他浏览器,因此不知道该怎么做。任何帮助都会很棒!
web视图
铬
public class Website extends Activity {
WebView myWebView;
LinearLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://dspart.org");
root = (LinearLayout) findViewById(R.id.root);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()) {
myWebView.goBack();
}
else {
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}}
答案 0 :(得分:1)
YourWebView.getSettings().setJavaScriptEnabled(true);
尝试在您的网页浏览中启用javascript!上面的代码段:
答案 1 :(得分:0)
似乎以下行中存在错误 if(Uri.parse(url).getHost()。equals(“http://dspart.org”))
getHost将返回dspart.org而不是带有http的网址。修改后尝试一下。感谢
参考 - http://developer.android.com/reference/android/net/Uri.html#getHost()
答案 2 :(得分:-1)
尝试浏览网站,不是浏览CHROME浏览器,而是浏览默认浏览器。
当我默认浏览蝴蝶浏览器(HTL21)时,您的网站已经崩溃。
尝试检查CSS和HTML。