Reputation: 33
I am trying to extract objects and lists created in JavaScript to use them inside Android application. (I have succeeded in extracting single values.) I am using addJavaScriptInterface method to implement this.
Inside test.html, I have the following script code: (I've tried without ".slice()" but did not work either)
function getList(){
var categoryTotals = {};
categoryTotals[0] = 1;
categoryTotals[1] = 2;
categoryTotals[2] = 3;
return categoryTotals.slice();
}
And the WebViewClient's onPageFinished method contains the following code:
mWebView.loadUrl("javascript:window.HTMLOUT.callAndroidList(getList());");
My JavaScriptInterface has the following function:
public void callAndroidList(final List list){
myList = list;
Log.d("ListTest" , "LIST 1 >>>>>>>>>>>>> " + ListTest.myList.get(0));
Log.d("ListTest" , "LIST 2 >>>>>>>>>>>>> " + ListTest.myList.get(1));
Log.d("ListTest" , "LIST 3 >>>>>>>>>>>>> " + ListTest.myList.get(2));
}
When I run this code, I get NullPointerException saying that callAndroidList's parameter, list, is null. I haven't dealt much with JavaScript so I think it is possible that this is related to creation and removal of JavaScript object instances.
Could you help me out? Thanks in advance.
Upvotes: 2
Views: 304
Reputation: 147473
> function getList(){
> var categoryTotals = {};
That will create an object, I think you mean to create an array, so perhaps it should be:
var categoryTotals = [];
.
> categoryTotals[0] = 1;
> categoryTotals[1] = 2;
> categoryTotals[2] = 3;
> return categoryTotals.slice();
Since you are creating an object with numeric properties, not an array, it does not have a slice method. Initializing categoryTotals as an array should fix that.
Incidentally, there does not seem to be any point in using slice to return a copy of the array. Since categoryTotals is not used for anything else, why not just return it?
Upvotes: 1