簡體   English   中英

在Titanium Apperator中無法讀取null的屬性長度

[英]Can not read property length of null in titanium appcelerator

我正在使用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(); 

你能幫我嗎

提前致謝

嘗試將結果命名為“ json”以外的名稱。

此外,還有草率的var聲明...

“ var Json,Story;” <<“ J” son大寫,從未使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM