简体   繁体   English

自动填写WebView android中的字段

[英]Auto fill in fields in WebView android

i want to fill in fields with this code but it gives me error 我想用这段代码填写字段,但它给了我错误

Account[] accounts = AccountManager.get(this).getAccounts();
    for (Account account : accounts) {
      // TODO: Check possibleEmail against an email regex or treat
      // account.name as an email address only for certain account.type values.


      String possibleEmail = account.name;



    myWebView.setWebViewClient(new HelloWebViewClient());

    setTitleColor(Color.GRAY); //the title bar color
    myWebView.loadUrl("http://m.gmail.com");
    myWebView.loadUrl("javascript: {document.getElementById('id_email').value ='"+possibleEmail+"'};");


} 

It gives me this error 它给了我这个错误

05-02 20:28:24.191: E/Web Console(1402): Uncaught TypeError: Cannot set property 'value' of null at :1 05-02 20:28:24.191:E / Web控制台(1402):未捕获的TypeError:无法在以下位置设置null的属性'value':1

Thanks for your help 谢谢你的帮助

loadURL is asynchronous; loadURL是异步的; in other words, calling loadUrl kicks off the HTTP request, but then returns immediately, before the document is downloaded and rendered. 换句话说,调用loadUrl启动HTTP请求,但在文档下载和呈现之前立即返回。 The error you're seeing is because you're trying to execute the javascript too early (before the document is downloaded, parsed, and rendered). 您看到的错误是因为您尝试过早地执行javascript(在下载,解析和呈现文档之前)。

You'll need to use a WebViewClient , and wait for the page to finish loading before you execute your javascript. 您需要使用WebViewClient ,并在执行javascript之前等待页面完成加载。 Try the onPageFinished callback, but be careful that you can sometimes get multiple callbacks because of iFrames and the like. 尝试onPageFinished回调,但要注意,由于iFrames等,有时可以获得多个回调。

Hope that helps. 希望有所帮助。

EDIT If you are targeting KITKAT and above then you also have to take care of which method to use based on android version. 编辑如果您的目标是KITKAT及以上,那么您还必须根据Android版本来处理使用哪种方法。 Follwoing code give a much better explanation: Follwoing代码提供了更好的解释:

 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
 {
        webView.evaluateJavascript(yourScript, null);
 }
 else
 {
       webView.loadUrl(yourScript);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM