Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-14545] Android: Accessing empty ListView section crashes app

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionDuplicate
Resolution Date2014-04-09T21:48:21.000+0000
Affected Version/sRelease 3.2.0
Fix Version/sn/a
ComponentsAndroid
Labelsandroid, listview, parity
ReporterMark Mokryn
AssigneeHieu Pham
Created2013-07-12T11:04:21.000+0000
Updated2017-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();

Comments

  1. Daniel Sefton 2013-07-12

    Tested and confirmed on Samsung Galaxy S2 2.3.6 with Ti SDK 3.2 CI.
  2. Tadatoshi Hanazaki 2013-12-12

    I also. Galaxy S4 4.2.2 with Ti SDK 3.1.3
  3. Mark Mokryn 2014-04-09

  4. Lee Morris 2017-03-20

    Closing ticket as duplicate.

JSON Source