Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;“未定义”;从Android向JavaScript传递数据时_Javascript_Android_Android Webview - Fatal编程技术网

&引用;“未定义”;从Android向JavaScript传递数据时

&引用;“未定义”;从Android向JavaScript传递数据时,javascript,android,android-webview,Javascript,Android,Android Webview,我正在使用这段代码,但是值是“undefined”,有人能告诉我这个问题吗 这是我的java类代码 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(com.frux.web.R.layout.activity_main); String value = "Isiah"; WebView web = (WebVie

我正在使用这段代码,但是值是“undefined”,有人能告诉我这个问题吗

这是我的java类代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.frux.web.R.layout.activity_main);
    String value = "Isiah";
    WebView web = (WebView) findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("file:///android_asset/www/index.html");
    web.loadUrl("javascript:setValue(\""+ value +"\")");


}
这是我的网页代码

<!DOCTYPE html>
<html>
<head>
</head>
<body>

Whats your Name?
<input id="name" value="" />
<button onclick = "setValue()">Submit</button>

<script type="text/javascript"> 
function setValue(value){
var myValue = value;
document.getElementById("name").value = myValue;

}
</script>
</body>
</html>
如有任何想法,将不胜感激

我使用了这个HTML代码中的代码,它的显示与我上面发布的其他代码没有什么不同

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">  

function setValue(amount1)
{
    myValue = amount1;
    document.getElementById("amount").value = myValue;
  }

function rand ( n )
{
    document.getElementById("orderRefId").value =  ( Math.floor ( Math.random ( ) * n + 1 ) );
}



</script>
</head>
<body onLoad="rand(200000);setValue();">
        <!-- 
            Note: https://www.pesopay.com/b2c2/eng/payment/payForm.jsp for live payment URL
                  https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp for test payment URL
        -->
        <form method="POST" name="frmPayment" action="https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp">
        <table>
        <tbody>
        <tr>
            <td>Order Reference No. (your reference number for every transaction that has transpired):</td> 
            <td><input type="text" id="orderRefId" name="orderRef" value="Test-001"/></td>
        </tr>   
        <tr>
            <td>Amount:</td>
            <td><input type="text" name="amount" id="amount" value=""/></td>

        </tr>
        <tr>
            <td>Currency Code - "608" for Philippine Peso, "840" for US Dollar:</td>
            <td><input type="text" name="currCode" value="608"/></td>
        </tr>   
        <tr>
            <td>Language:</td>
            <td><input type="text" name="lang" value="E"/></td>
        </tr>   
        <tr>
            <td>Merchant ID (the merchant identification number that was issued to you - merchant IDs between test account and live account are not the same):</td> 
            <td><input type="text" name="merchantId" value="18056869"/></td>
        </tr>
        <tr>    
            <td>Redirect to a URL upon failed transaction:</td>
            <td><input type="text" name="failUrl" value="http://www.yahoo.com?flag=failed"/></td>
        </tr>   
        <tr>
            <td>Redirect to a URL upon successful transaction:</td>
            <td><input type="text" name="successUrl" value="http://www.google.com?flag=success"/></td>
        </tr>
        <tr>
            <td>Redirect to a URL upon canceled transaction:</td>
            <td><input type="text" name="cancelUrl" value="http://www.altavista.com?flag=cancel"/></td>
        </tr>
        <tr>
            <td>Type of payment (normal sales or authorized i.e. hold payment):</td> 
            <td><input type="text" name="payType" value="N"/></td>
        </tr>
        <tr>    
            <td>Payment Method - Change to "ALL" for all the activated payment methods in the account, Change to "BancNet" for BancNet debit card payments only, Change to "GCASH" for GCash mobile payments only, Change to "CC" for credit card payments only:</td>
            <td><input type="text" name="payMethod" value="ALL"/></td>
        </tr>
        <tr>    
            <td>Remark:</td>
            <td><input type="text" name="remark" value="Asiapay Test"/></td>
        </tr>
        <!--<tr>    
            <td>Redirect:</td>
            <td><input type="text" name="redirect" value="1"/></td>
        </tr>-->
        <tr>
            <td></td>
        </tr>   

        <input type="submit" value="Submit">




        </tbody>
        </table>    


        </form>

</body>
</html>             

函数设置值(数量1)
{
myValue=数量1;
document.getElementById(“金额”).value=myValue;
}
函数rand(n)
{
document.getElementById(“orderRefId”).value=(Math.floor(Math.random()*n+1));
}
订单参考号(发生的每笔交易的参考号):
数量:
货币代码——菲律宾比索为“608”,美元为“840”:
语言:
商户ID(发给您的商户识别号-测试账户和live账户之间的商户ID不相同):
在事务失败时重定向到URL:
成功交易后重定向到URL:
在取消事务时重定向到URL:
付款类型(正常销售或授权,即保留付款):
付款方式-对于账户中所有激活的付款方式,更改为“全部”;对于BancNet借记卡付款,更改为“BancNet”;对于GCASH移动支付,更改为“GCASH”;对于信用卡付款,更改为“CC”:
备注:

尝试将您的值用单引号括起来,如下所示

 web.loadUrl("javascript:setValue('"+ value +"')");

将传递的值用双引号括起来:

web.loadUrl("javascript:setValue(\""+value+"\")");
我明白了!当您第二次调用
loadUrl
时,页面尚未加载。解决方案是将
setValue
调用附加到
窗口。onload
事件:

super.loadUrl("javascript:window.onload = function(){setValue(\"haha\");};");

这段代码正确地将“哈哈”加载到输入中。

在DOM准备就绪之前,我试图访问DOM时遇到了这个问题。devmilles.com的解决方案奏效了,但我花了更多的时间进行试验

两者

webView.loadUrl("javascript:window.addEventListener('DOMContentLoaded',
    function(){setValue('haha');}, false);");

webView.loadUrl("javascript:window.addEventListener('load',
    function(){setValue('haha');}, false);");
为我工作,但是将事件附加到
文档
而不是
窗口
没有,在受支持的情况下,响应速度略快于

此外,事实证明,
WebViewClient
总是有一个事件,它的工作方式与
load
事件类似

webView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:setValue('haha');");
    }
});
最后,我可以模仿这种行为

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        if (progress == 100) {
            view.loadUrl("javascript:setValue('haha');");
        }
    }
});

但是我认为没有理由通过
WebViewClient
方法来使用它(它们可能是等效的,也可能不是等效的)。

我也面临着同样的问题。但问题是通过以下方法解决的

     String testValue="abcdedfg";
    String value = "\'"+testValue+"\'";
    web.loadUrl("javascript:setValue(\""+value+"\")");

您没有为
setValue()
value
参数传递任何值,因此它是未定义的。这是什么意思?你能告诉我你的意思吗?
setValue()
需要传递一个参数,
value
。它是通过点击按钮来调用的,但没有传递任何参数-即,您正在通过
setValue()
调用没有参数的函数,您可以给我任何链接,或者您可以帮助我做您试图指出的事情吗?或者您可以告诉我我缺少什么是我的代码吗?那将是我的荣幸。你真是太棒了,伙计!但是,为什么调用setValue函数时会自动将值放入文本框中呢?因为
setValue
函数就是这样做的。当我使用onLoad或onClick调用函数setValue()时,它仍然没有定义。是否将参数传递给setValue?因为在你的问题代码中,你不能告诉我我缺少什么,可以在我的网页中使用onClick或onLoad调用setValue()函数。
     String testValue="abcdedfg";
    String value = "\'"+testValue+"\'";
    web.loadUrl("javascript:setValue(\""+value+"\")");