Pushing a text string onto an array causes error..
I am running Android 2.2 with Titanium mobile 1.51.
I have an application scoped array in my app.js file "textArray". As part of the creation of the CreateWindow (main window for the application) I pass the array as a property to the CreateWindow function.
var textArray = new Array();
var RootWindow = Titanium.UI.createWindow({
title:'Root Window',
url:urlToLoad,
textArrayIn:textArray
});
Internally following code logic that wishes to add an entry to the array from one of the views used in the application, I use the property of the window and assign by reference to variable. Upon running the "push" method on the array, I get the following error:
12-28 16:43:33.029: ERROR/KrollCallback(24355): (kroll$4) [713,305148] ECMA Error evaluating source, invocation: [callMethod Network.HTTPClient.(anonymous) org.appcelerator.titanium.kroll.KrollCallback@43f72b08], message: TypeError: Cannot find default value for object. (app://menus/display_item.js#60)
12-28 16:43:33.029: ERROR/KrollCallback(24355): org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object. (app://menus/display_item.js#60)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3761)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3739)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3767)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3779)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:839)
12-28 16:43:33.029: ERROR/KrollCallback(24355): at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:758)
The code that breaks is as follows:
In a separate function that displays some ui controls, input text is attempted to be stored into the array as follows:
var win = Titanium.UI.currentWindow;
var textArrayIn = win.textArrayIn;
textArrayIn.push(newText);
I have traced adding an array element at the application level and output the array length (1) prior to the "push" call and also traced the value in the textArrayIn[0] position. Both worked as expected, so Array looks fine to access (i.e. apparently no memory issues)
I also tested pushing pure text 'test' etc. onto the textArrayIn array which triggered the same error.
When I create a separate local array as follows the push works fine, so it appears to be some scoping issue:
var key = 'test';
var newArray = new Array();
newArray.push(key);
Any help on this would be really appreciated.
J.
2 Answers
-
Hi,
Wold you can describe sdk version?
i create this code and works correctly:
app.js:
var textArray = []; textArray.push('a'); textArray.push('b'); textArray.push('c'); var RootWindow = Titanium.UI.createWindow({ title:'Root Window', url:'a.js', textArrayIn:textArray }); RootWindow.open();a.js:
var win = Titanium.UI.currentWindow; var textArrayIn = win.textArrayIn; alert(textArrayIn); textArrayIn.push('newText'); alert(textArrayIn); -
Hi, I'm having a similar problem in Android (Galaxy Tab, 2.2) when parsing xml to json.
The code below works fine in iOS but fails in Android with error: TypeError: Cannot find function push in object .(app://getAccounts.js#111)
Line #111 is:
obj[nodeName].push(xmlToJson(item));
(6th line bottom-up below)Full function below, any clues much appreciated!
function xmlToJson(xml) { // Create the return object var obj = {}; if (xml.nodeType == 1) { // element // do attributes if (xml.attributes.length > 0) { obj["@attributes"] = {}; for (var j = 0; j < xml.attributes.length; j++) { var attribute = xml.attributes.item(j); obj["@attributes"][attribute.nodeName] = attribute.nodeValue; } } } else if (xml.nodeType == 3) { // text obj = xml.nodeValue; } // do children if (xml.hasChildNodes()) { for(var i = 0; i < xml.childNodes.length; i++) { var item = xml.childNodes.item(i); var nodeName = removeNS(item.nodeName); if (typeof(obj[nodeName]) == "undefined") { obj[nodeName] = xmlToJson(item); } else { if (typeof(obj[nodeName].length) == "undefined") { var old = obj[nodeName]; obj[nodeName] = []; obj[nodeName].push(old); } obj[nodeName].push(xmlToJson(item)); } } } return obj; };