0

I am trying to fill some fields in my android web view. but all the time I have this error 01-28 05:06:22.980: E/Web Console(2827): Uncaught TypeError: Cannot set property 'value' of null at null:1

I have searched but didnt find some solution. How can I resolve this? Here is my code

public void onStart() {
        super.onStart();

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());

        String url = "file:///android_asset/index.html";
        webView.loadUrl(url);

        webView.setWebViewClient(new WebViewClient(){

               public void onPageFinished(WebView view, String url) {
                   webView.loadUrl("javascript: {"
                            + "document.getElementById('AddSerialNum').value = '"
                            + ABC
                            + "';"
                            + "document.getElementById('ctl00_PageContent_lblName').value = '"
                            + ABC + "';};");

                }
            });
    }

this is my index.html file

<!DOCTYPE html>
 <html>

 <body>

 <div style="BORDER-BOTTOM: #dfdfdf 3px solid; PADDING-BOTTOM: 1px; MARGIN-BOTTOM: 10px; HEIGHT: 25px;">
               ......          
                <div style="padding: 10px;">
                    <table width="100%">
                        <tbody><tr>
                            <td>
                                <div style="float: left;">
                                    <div>

                                    <form>Serial Number: <input type="text" name="AddSerialNum"></form><br><br>

                                        <span id="ctl00_PageContent_lblName" style="font-weight:bold;"></span>
                                    </div>......

Is there any way to fill the fields and get data later with java?

Thanks in advance.

2 Answers 2

3

With getElementById you will get an DOM Element by id. The id AddSerialNum does not exist, this is what the error tells you. You have an input with name="AddSerialNum", which, i think you want to get. Either add an id to it, or use getElementsByName("AddSerialNum")[0]

EDIT: Like i already pointed out, you have to use this:

webView.loadUrl("javascript: {document.getElementsByName(\"AddSerialNum\")[0].value ='" + ABC + "';};");

The getElementsByName returns an array of elements, you have to get the first one with [0]

3
  • 3
    I have tried this webView.loadUrl("javascript: {document.getElementsByName(\"AddSerialNum\").value ='" + ABC + "';};"); but my field is still empty
    – yakhtarali
    Jan 28, 2014 at 10:35
  • hey . can you please refer to any full example. I have not worked with this kind of code before . Apr 17, 2016 at 6:35
  • @bbuecherl please look at this stackoverflow.com/questions/37644551/… . it needs your help . Jul 23, 2016 at 4:41
2

Just to add a little bit more of code as sample.

    mWebView.setWebViewClient(new WebViewClient() {
            private void setUserEmail(WebView view) {
                String js = "javascript: {document.getElementsByName(\"recover-email\")[0].value ='" + mEmail + "';};";
                if (Build.VERSION.SDK_INT >= 19) {
                    view.evaluateJavascript(js, s -> {
                    });
                } else {
                    view.loadUrl(js);
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mLoadingProgressBar.setVisibility(View.GONE);
                setUserEmail(view);
            }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.