简体   繁体   中英

ID attribute returns null on android device with Titanium

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 add method you are using to add rows in table.

To add a row to a tableView you can use following two methodology :

  1. Use appendRow method of tableView : Replace your $.table.add(row); with $.table.appendRow(row);

  2. Push all the rows in an array and then add it to tableView using setData method. Your code will look something like :

     var dataRows = []; 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); } dataRows.push(row); } // add TableViewRows to table $.table.setData(dataRows); 

Hope it helps.

This may be a “shot in the dark” - but I had a problem with an id called “private”. I had to rename my id to “isPrivate” - so I guess some of these cannot be “reserved names”. I have not checked this - just found out by quick trial-and-error. But perhaps “table” is a similar reserved word?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM