在cnblogs的android用戶端開中使用webview類比登入,這樣就需要將使用者輸入的使用者名稱和密碼傳到webview上然後提交,這個過程在mx3的flyme3和flyme4上測試一切正常,然而在我的htcG12上(用的miui4),和三星note2上都報錯。
報錯的代碼為:
| 代碼如下 |
複製代碼 |
|
public void LoginByPassword(String username, String password){
mCurrentRT = EnumRequestType.LoginFinish;
final String strJS = String.format("javascript:document.getElementById('tbUserName').value='%s';document.getElementById('tbPassword').value='%s';document.getElementById('form1').submit();", username, password);
mWeb.post(new Runnable() {
public void run() {
mWeb.loadUrl(strJS);
}
});
}
|
mWeb.loadUrl(strJS);時報錯了,報錯的內容為“android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application”。
解決辦法,是對mWeb的settings進行設定不儲存密碼“settings.setSavePassword(false);”代碼如下:
| 代碼如下 |
複製代碼 |
@SuppressLint({ "SetJavaScriptEnabled"})
private void initialWeb(){
WebSettings settings = mWeb.getSettings();
settings.setSavePassword(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);
settings.setBlockNetworkImage(true);
mWeb.addJavascriptInterface(new JSInterface(), "IFObj");
mWeb.setWebViewClient(mBlogWebClient);
}
|
就此,原因找到了,不過“Unable to add window -- token null is not for an application”的錯誤,並不都是這個導致的。