Use webview to simulate logon on the android client of cnblogs. In this way, the user name and password entered by the user must be uploaded to the webview and submitted. This process is normal on flyme3 and flyme4 of mx3, however, on my htcG12 (miui4 used) and Samsung note2, an error is reported.
The error code is:
The code is as follows: |
Copy code |
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); the error message is "android. view. WindowManager $ BadTokenException: Unable to add window -- token null is not for an application ".
The solution is to set mWeb settings without saving the password "settings. setSavePassword (false:
The code is as follows: |
Copy code |
@ 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 ); }
|
This is the reason. However, this is not the case if the error "Unable to add window -- token null is not for an application" is found.