我试图从我的javascript代码调用java android代码中的函数。 我收到以下错误:
"Uncaught TypeError: Object [object Object] has no method 'calculateSum'", source: http://www.abelski.com/courses/android/simple.html"
Javascript代码:
<script>
function calc()
{
var a = parseInt(document.myform.num_a.value,10);
var b = parseInt(document.myform.num_b.value,10);
var sum = window.ob.calculateSum(a,b);
document.myform.result.value = sum;
}
</script>
<body>
<form name="myform">
<br/>number 1: <input type="text" name="num_a"/>
<br/>number 2: <input type="text" name="num_b"/>
<br/><input type="button" onclick="calc()" value="+"/>
<br/>result: <input type="text" name="result"/>
</form>
</body>
java android code:
public void onCreate(Bundle savedInstanceState)
{
CalculateObject calcObject = new CalculateObject();
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.loadUrl("http://www.abelski.com/courses/android/simple.html");
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(calcObject, "ob");
setContentView(webView);
}
class CalculateObject
{
public int calculateSum(int numA, int numB)
{
return numA + numB;
}
}