Solving WebView Password control assignment problems with JavaScript on Android

Source: Internet
Author: User
Tags error code

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.