[英]Android blank WebView after Application Restart
我的WebView有問題。 當我使用Eclispe啟動應用程序時,它在我的設備(三星Galaxy S2,Android 4.1.2 / API 16)上運行良好。 它使用webview.loadUrl('my url');加載我通過的網站; 並掌握了一些JavaScript警報和相關內容。 當我讓應用程序重新啟動時,它只能再次運行。 當我第二次重新啟動應用程序時,它不會加載任何URL。 我想這與saveInstanceState有關,但是即使我在Activity的開始將Bundle設置為null,它也不會重新加載URL。
protected void onCreate(Bundle savedInstanceState) {
savedInstanceState=null;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.browser);
...
browser.loadUrl(url);
}
我也嘗試刪除所有帶有
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
仍然有相同的錯誤。 我已經閱讀了很多有關Android中WebView的已解決問題和問題,但沒有任何幫助。 請問有任何想法嗎? 我很困惑...
整個onCreate方法:
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
savedInstanceState=null;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.browser);
// if (savedInstanceState != null) {
// browser.restoreState(savedInstanceState);
// }
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);
final Context myApp = this;
final Communicator c = new Communicator();
/* WebChromeClient must be set BEFORE calling loadUrl! */
browser.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
final android.webkit.JsResult result) {
Log.d(TAG, "onJsAlert");
try {
JSONObject json = new JSONObject(message);
if (json.has("func")) {
if (json.getString("func").equals("onUserInfos")) {
c.onUserInfos(json);
Log.d(TAG, "JSON verarbeitet");
}
}
return true;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO
Log.v(TAG, "JavaScript Alert empfangen");
new AlertDialog.Builder(myApp)
.setTitle("javaScript dialog")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
}).setCancelable(false).create().show();
return false;
};
});
// Reset all Cookies
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
/* load a web page which uses the alert() function */
String url = c.getGuiURL();
browser.loadUrl(url);
}
當您離開應用程序時,該過程可能會繼續運行。 因此,當您返回到應用程序時,將調用onResume方法而不是onCreate。
在android開發人員網站上查看此頁面,以獲取有關活動生命周期的更多信息http://developer.android.com/guide/components/activities.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.