Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

456
Visualizações
Add event listener in webview.evaluateJavascript() function in android

i am trying to run script after page load finish in android web view to add clickevent on button

     var btn = document.getElementById("btn2");
    document.getElementById("btn2").addEventListener("click", myFunction);
                            function myFunction() {
     AndroidInterface.showToast2();
    window.console.log("inside ");
    }

       

This is my html file code

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <title>Hello</title>

    <style>
      body, html {
        height: 100%;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #F89821;
        background-color: #ffffff;
        padding: 20px;
        margin-bottom: 100px;
      }
    </style>

</head>
<body>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<input type="button" id="btn2" value="Say hello2" />

<br/><br/>
<input type="button" value="Show Version" onClick="showVersion('called within the html')" />
<br/><br/>
<p id="version"></p>


    <script type="text/javascript">
    
    window.showAndroidToast =
    
            function (toast) {
                AndroidInterface.showToast(toast);
            }
    
            <!-- Getting value from Android -->
            function showVersion(msg) {
                var myVar = AndroidInterface.getAndroidVersion();
                document.getElementById("version").innerHTML = msg + " You are running API Version " + myVar;
    
    
            }
          </script>
    </body>
    </html>

in console log i am getting this error Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at :6:38

Which i believe document.getElementById() not able to find element.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda