开发者

Error using getAttribute, why?

I am using Appcelerator Titanium for an Android app and the code below generates an error in the Android emulator, not sure if its my Javascript OR something in Appcelerator.

It objects to the getAttribute for some reason and give the following error:

TypeError: Cannot call method "getAttribute" of undefined (app://pages/xml.js#12)

(it may not be line #12 below due to formatting!! Thx)

Can anyone shed any light onto why the getAttribute is causing a problem?

Here is the code:

// BEGIN SAMPLE CODE

var xhr = Titanium.Network.createHTTPClient();

xhr.onload = function() 
   { 
      var xmlDoc = this.responseXML.documentElement;
      var xlocalestatus=xmlDoc.getElementsByTagName('Locations');
      var xbooks=xmlDoc.getElementsByTagName('Books');
      var newname = '';

      for (i=0;i<xlocalestatus.length;i++) 
         { 
          newname = xbooks[i].getAttribute('Name');
          Ti.API.info(newname);
         }
   };

// open 开发者_StackOverflow中文版the client

xhr.open('GET','http://myurl.com');

// send the data

xhr.send();


// END SAMPLE CODE

Code also available on Pastie: here http://pastie.org/1670908

Thanks


It's saying you're calling getAttribute on an undefined value, that is, xbooks[i] is undefined.

This probably indicates that you're not getting the expected result returned from the server you're contacting. You should examine exactly what's coming back, and exactly what xbooks and/or its elements are being set to.


You are expecting the xlocalestatus and xbooks are having the same length. Probably the actual lengths of these 2 arrays are different. Say the first one is of 10 and the second one is of 8. Now when you are accessing the xbooks[9] you will get this error and the xbooks[9] is undefined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜