无法在钛appcelerator中读取null的属性长度

时间:2013-02-14 22:45:26

标签: android titanium

我正在使用titanium检索json数据。但问题是每当我计算json数据的长度时,它表示无法读取null的长度这是我的代码:

                  Ti.UI.setBackgroundColor('#000');

 var win1 = Ti.UI.createWindow({
backgroundColor : '#fff',
title : 'Tab 1'
 });
var tab1 = Ti.UI.createTab({
icon : 'KS_nav_views.png',
title : 'Tab 1',
window : win1
 });

var JsonTable = Ti.UI.createTableView({
height : Ti.UI.FILL,
width : Ti.UI.FILL
});
win1.add(JsonTable);

 function getData() {
var myfontsize = '14dp';
// I needed to add this to test
var tableData = [];
var Json, Story;
var xhr = Ti.Network.createHTTPClient({
    onload : function() {
        var json = JSON.parse(this.responseText);
        // declare your variables :-)
        //alert(JSON.stringify(Story));
    //  alert(Story.nombre);
        alert(json.length);
        // only one . between things
        for (var i = 0; i < json.length; i++) {
            Story = json[i];
            //Ti.API.info('Story', JSON.stringify(Story));
            //Ti.API.info('Story.nombre', Story.nombre);
            var row = Ti.UI.createTableViewRow({
                backgroundColor : 'yellow',
                height : '85dp',
                width : Ti.UI.FILL
            });
            var labTitle = Ti.UI.createLabel({
                backgroundColor : 'orange',
                color : 'black',
                font : {
                    fontSize : myfontsize,
                    fontWeight : 'bold'
                },
                height : Ti.UI.SIZE,
                left : '25%',
                text : Story.nombre,
                top : '2%',
                width : Ti.UI.FILL
            });

            row.add(labTitle);
            tableData.push(row);
        }
        JsonTable.setData(tableData);
    },
    onerror : function(e) {
        Ti.API.debug("STATUS: " + this.status);
        Ti.API.debug("TEXT: " + this.responseText);
        Ti.API.debug("ERROR: " + e.error);
        alert('There was an error retrieving the remote data. Try again.');
    },
    timeout : 5000
});
JsonTable.addEventListener('click', function(e) {
    var index = e.index;
});
xhr.open("GET", "http://aplicaciones4.sct.gob.mx/sibuac_internet/SerEscogeRuta?estados");
xhr.send();
  }

 var btnGo = Ti.UI.createButton({
title : 'Go'
 });
 btnGo.addEventListener('click', function(e) {
getData();
  });
  //win1.setRightNavButton(btnGo);
win1.add(btnGo);

var img = Ti.UI.createImageView({
height : 32,
image : 'activity_indicator.gif',
width : 32
});
//win1.add(img);

 var tabGroup = Ti.UI.createTabGroup();
 tabGroup.addTab(tab1);
 tabGroup.open(); 

你能帮助我吗

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试将结果命名为“json”以外的其他内容。

此外,还有粗糙的var声明......

“var Json,故事;” &LT;&LT; “J”儿子大写,从未使用

相关问题