BlackBerryForums.com : Your Number One BlackBerry Community
     

»Sponsored Links


BlackBerryApps.com Best Sellers



Reply
 
LinkBack Thread Tools
  (#1 (permalink)) Old
rhen.sn Offline
New Member
 
Posts: 3
Join Date: Jun 2009
Model: 8900
PIN: N/A
Carrier: XL
Default Having trouble with FolderListener and ListField - 07-02-2009, 09:40 AM

Hi all, I'm a newbie on developing application in Blackberry. I have some issues when I try to add items to my ListField.

I've created a class that extends ListField and implements FolderListener interface. All I want is listening to the device inbox , when there a message arrive, I want to append it into my ListField class. Here is my code:

Code:
public void messagesAdded(FolderEvent e) {
Store store = Session.getDefaultInstance().getStore();
        Folder[] f = store.list(Folder.INBOX);
        if (f != null && f.length > 0) {
            try {
                //retrieve only the first inbox
                Folder _inbox = f[0];

                Message _msgInbox[] = _inbox.getMessages();
                for (int i = 0; i < _msgInbox.length; i++) {
                    if (_msgInbox[i].getMessageType() == Message.EMAIL_MESSAGE) {
                        System.out.println("Sender :" + _msgInbox[i].getFrom().getName());
                        data.insertElementAt(_msgInbox[i], i);
                        this.insert(i);
                    }
                }
                this.invalidate();
                size = data.size();
                System.out.println("Data size :"+size);

            } catch (MessagingException ex) {
                ex.printStackTrace();
            } catch(IllegalStateException ex){
                System.out.println(ex.getMessage());
            }
        } 
}

I got an
Code:
 IllegalStateException
when inserting the data
(
Code:
this.insert(i)
)


Can someone give me a clue why this happened ?
   
Reply With Quote
Sponsored Links
Please Login or Register to Remove these Advertisements!

  (#2 (permalink)) Old
Dougsg38p Online
CrackBerry Addict
 
Posts: 799
Join Date: Mar 2008
Location: Austin, TX
Model: 8320
PIN: N/A
Carrier: T-Mobile
Default 07-02-2009, 01:44 PM

The UI is a single-entrant single-threaded component, and can only be accessed from the event thread.

The FolderListener thread actually belongs to another app (the message app).

A common practice is to:

1. Fire a custom global event to your application, which implements GlobalEventListener
2. From the GlobalEventListener, create a runnable object that performs the UI operation, and call it using UiApplication.invokeLater().
   
Reply With Quote
  (#3 (permalink)) Old
rhen.sn Offline
New Member
 
Posts: 3
Join Date: Jun 2009
Model: 8900
PIN: N/A
Carrier: XL
Default 07-06-2009, 03:17 AM

thx doug...... I think you are right. I moved the listener into my main App and it suddenly works... But now I'm facing different problem. When I tested and run it in 9530 simulator, it works fine. But when I deployed on actual device I got java.lang.NullPointerException


Here is my main application code :
Code:
public class App extends UiApplication implements FolderListener {

    private MailBoxScreen _main;

    public static void main(String[] args) {
        App instance = new App();
        instance.enterEventDispatcher();
    }

    /** Creates a new instance of App */
    public App() {
        _main = new MailBoxScreen(this);
        pushScreen(_main);
    }

    public void messagesAdded(FolderEvent e) {
        //TODO: append to the vector
        final Message incoming = e.getMessage();
        if (incoming != null) {
            try {
                while (App.this == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                App.this.invokeLater(new Runnable() {
                    public void run() {
                        //Dialog.alert("1 new mail received");
                        LED.setState(LED.STATE_BLINKING);
                        Alert.startVibrate(2000);
                        MailBoxScreen.getMailListInstance().refreshMail(incoming);
                    }
                });
            } catch (NullPointerException ex) {
            }
        }
    }

I also has surround each code on method that called from the main application with Exception. Still cannot find where i got the NullPointerException
   
Reply With Quote
  (#4 (permalink)) Old
Dougsg38p Online
CrackBerry Addict
 
Posts: 799
Join Date: Mar 2008
Location: Austin, TX
Model: 8320
PIN: N/A
Carrier: T-Mobile
Default 07-06-2009, 11:14 AM

Are you running on a 9530 device?

Look at the event log on the device and get the stack trace.

On a trackball device:

<alt> L G L G

On a Storm:

press and hold <?123>, then type " , 5 , 5 "
   
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2009 BlackBerryFAQ.com, BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of Research In Motion Limited.