我遇到了使用loadDataWithBaseURL方法加载网站的问题。我在webview中加载了准备好的internetsite。如果我回到之前的活动并开始webview活动,它会使用loadDataWithBaseURL()方法加载相同的准备好的网站,但我得到一个内容两倍的webview。例如Hello World和第二个Hello World。 我的代码如下:
package de.http.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mywebview);
Bundle bundle= this.getIntent().getExtras();
String param1 = bundle.getString("Weburl");
getDetailWebsite(param1);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
loadWebview();
}
public void loadWebview()
{
webview.clearView();
webview.loadDataWithBaseURL("http://branchenportal-schlitz.de", HttpHelperClass.htmlDetailWebsiteCode, "text/html", "UTF-8", HttpHelperClass.htmlDetailWebsiteCode);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()).getUrl();
if(historyUrl.contains("<html>"))
{
loadWebview();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void getDetailWebsite(String url)
{
try{
HttpHelperClass.executeHttpGetDetailWebsite(url,this);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(url.contains("branchenportal-schlitz.de"))
{
view.clearHistory();
}
super.onPageFinished(view, url);
}
}
@Override
protected void onStop() {
webview.clearCache(true);
webview.clearView();
webview.clearHistory();
webview.destroy();
super.onStop();
}
}
有谁知道我做错了什么? 提前致谢。 关于
答案 0 :(得分:0)
好的,我很难解决这个问题,我首先要删除(注释掉)所有代码,然后在公共布尔值onKeyDown(int keyCode,KeyEvent事件)下面。然后你可以看看它是否在你的onCreate中。但这取决于。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
final String urlToLoad = "http://www.yourWebsite.com";
wb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wb.loadUrl(urlToLoad);
}
从这里开始,然后开始添加额外的代码。