I searched on SO and others to find a solution in vain. I created a TableView in XML with an ID table and populated it dynamically (by creating several TableViewRows and TextFields into them).
The issue is the following: when I try to add all the created items programmatically with $.table.add(row), I have the expected result on browser but a NullPointerException on Android device.
I made some tests in my code to see what is returning null and found that the ID attribute $.table was the problem. What happen in Android and how to fix it ?
mytable.xml:
<Alloy>
<Window id="win_container">
<View id="wrapper">
<!-- The TableView -->
<TableView id="table" />
</View>
</Window>
</Alloy>
mytable.js:
for (...) {
// create TableViewRows
row = Ti.UI.createTableViewRow ({
className: "row",
...
});
// create several TextFields in one TableViewRow
for (...) {
tf = Ti.UI.createTextField ({
...
});
// add TextFields to the TableViewRow
row.add(tf);
}
// add TableViewRows to table
$.table.add(row); /// <----- '$.table' returns 'null'
}
Any help will be appreciated.
IMO the error is because of
addmethod you are using to add rows in table.To add a row to a
tableViewyou can use following two methodology :Use
appendRowmethod oftableView: Replace your$.table.add(row);with$.table.appendRow(row);Push all the rows in an array and then add it to
tableViewusingsetDatamethod. Your code will look something like :Hope it helps.