[TIMOB-14545] Android: Accessing empty ListView section crashes app
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Low |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2014-04-09T21:48:21.000+0000 |
Affected Version/s | Release 3.2.0 |
Fix Version/s | n/a |
Components | Android |
Labels | android, listview, parity |
Reporter | Mark Mokryn |
Assignee | Hieu Pham |
Created | 2013-07-12T11:04:21.000+0000 |
Updated | 2017-03-20T22:19:12.000+0000 |
Description
If a ListSection is not initialized with items, any access to it crashes Android app with null pointer exception. This makes loops such as
for (var i = 0; i < listView.sections[0].items.length; i++) { // ...
dangerous on Android, with annoying bookkeeping required. It works as expected on iOS, with no issue. See test code below:
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
var listView = Ti.UI.createListView({
top : '150dp'
});
var btn = Ti.UI.createButton({
top : '45dp',
width : '300dp',
height : '40dp',
title : 'Android crash items.length'
});
btn.addEventListener('click', function() {
listView.removeAllChildren();
var fruitSection = Ti.UI.createListSection({
headerTitle : 'Fruits'
});
var sections = [];
sections.push(fruitSection);
listView.sections = sections;
Ti.API.info('no items in section, Android will crash evaluating items.length in next line');
for (var i = 0; i < listView.sections[0].items.length; i++) {
Ti.API.info('this will not print');
}
alert('you will see this alert on iOS but not Android');
});
win.add(btn);
win.add(listView);
win.open();
Tested and confirmed on Samsung Galaxy S2 2.3.6 with Ti SDK 3.2 CI.
I also. Galaxy S4 4.2.2 with Ti SDK 3.1.3
Closing ticket as duplicate.