Question

I'm getting exception IllegalStateException :

Field added to a manager while it is already parented

When i call below function second time,when i am changing the value of spinner and this function is called second time, i am getting illegal state exception.

Here is my code :

void showSpinnerDialog(int type) {
    if (_customSpinnerPopup == null) {
        _customSpinnerPopup = new CustomSpinnerPopup();
    }

    UiApplication.getUiApplication().pushModalScreen(_customSpinnerPopup);

    if (_customSpinnerPopup.isSet()) {
        String choice = _customSpinnerPopup.getChoice();
        _editFieldSpinbox.setText(choice);
        getAlbumsForLanguage(choice);
    }
}

private void getAlbumsForLanguage(String choice) {
    language = choice;
    fieldManager.deleteAll();

    final RichList list = new RichList(fieldManager, true, 2, 1);
    songItemsList = new Vector();
    songItemsList = ServerAPI.getNewSongsForLanguage(language, null);
    for (int i = 0; i < songItemsList.size(); i++) {
        SongItem songItem = (SongItem) songItemsList.elementAt(i);
        list.add(new Object[] { bitmap1, songItem.getName(),
            "Artist:" + songItem.getArtist(),
        "Movie: " + songItem.getMovie() });
    }

    add(fieldManager);// **here i am getting exception**
    list.setFocusPolicy(TableController.ROW_FOCUS);

    list.setCommand(new Command(new CommandHandler() {

        public void execute(ReadOnlyCommandMetadata metadata, Object object) {
            SongItem song = (SongItem) songItemsList.elementAt(list.getFocusRow());
            Dialog.alert("exe !" + song.getName());
Was it helpful?

Solution

As the exception message suggests, you cannot add a Field or a Manager (which is a Field) to another container, more than once, unless you remove it first. When you call getAlbumsForLanguage() the second time, you will call this:

add(fieldManager);// **here i am getting exception**

a second time, which is illegal. To solve this, just surround that line with:

if (fieldManager.getManager() == null) {
   add(fieldManager);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top