Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers
Accepted Solution

Display Error (from different thread)

Hi,

 

I have a program which is downloading an xml file.

 

An error is encountered when I specifying a non-existent URL in the Http Connector (in order to produce an error).  I would like to display the error on a popup message, however the simulator is giving me an error when I attempt to display the message.

 

My code looks like this (abbreviated form):

 

 

1.  A Button Pressed
2.  Connection thread executed

...

private class Search extends Thread {
  public Search() {
	super();
  }

  public void run() {

     try {
     ...
     code for downloading and parsing file -- works just fine
     ...
     } catch (Exception e) {
String error = e.toString();
System.out.println(e.toString());

//remove the currently displayed "Please wait..." PopupScreen    
synchronized (UiApplication.getEventLock()) {
UiApplication.getUiApplication().popScreen(
UiApplication.getUiApplication().getActiveScreen());
				}
	
//display a new screen with the error				UiApplication.getUiApplication().pushScreen( new WaitScreen(error));
}

 

 

An error is thrown due to the last line "UiApplication.getUiApplication().pushScreen(new WaitScreen(error));'  I know this because when the line is removed, the program runs fine (with the exception of the fact that the file is not downloaded).

 

When the URL exists, there is no problem--the file is downloaded and the program does what I want it to do.  So, the problem is when I attempt to display a popup which shows the details regarding the error.

 

Anyone have any ideas why this might be the case?

 

Thanks!

 

 

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Display Error (from different thread)

 

UiApplication.getUiApplication().invokeLater(new Runnable()
   public void run() {

Status.show(error, 2000);
		       }
});

 

Found a solution to my problem:

 

 

I wasn't handling the threads properly.

 

Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Display Error (from different thread)

Hi,

  Actual problem is your thread. You cannot add and remove any fields and screen in your application with in a thread. If you try to do it, then in runtime it will throw IllegalArgument exception. try to push the screen from outside the thread, it will work.

with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Display Error (from different thread)

Hi Zany,

 

Thanks for responding!

 

I managed to track down a post which discussed  a similar topic.

 

The above code that I posted worked--I was able to display a popup from within the thread.

 

New Developer
Posts: 3
Registered: ‎01-07-2010
My Device: Not Specified

Re: Display Error (from different thread)

Hi Zany & B-Developed.. I am in the middle of this same problem.  Do you remember which post solved this for you?  I am trying to display a disclaimer in a dialog box the first time the app runs so it needs to appear on the mainscreen right at start up before any input or clicks are made... obviously I am getting the same runtime exceptions each time i start the app..

thanks!

Tom

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display Error (from different thread)

Probably better to start a new Thread and reference an old one, than add to a solved Thread, people often assume solved Threads are solved and need no further input.

 

In this case, can I suggest that when you push your first Screen, if the user has not already been through the app, then you push your disclaimer screen.  It will appear on top becuase it has been pushed second.  Just a thought.

New Developer
Posts: 17
Registered: ‎02-09-2010
My Device: Blackberry 9000
My Carrier: None

Re: Display Error (from different thread)

Greetings,

             I had a similar problem, I am connecting to the network directly before the main screen is displayed (in the OnuIEngineAttached() method), and if there is an error, I want to display an Alert dialog box. When this happens, i always get a RuntimeError (even when trying to use invokeLater())... Here is the Stack trace:

 

Thread [MEPDemo(163)id=1032396800] (Suspended (exception RuntimeException))    
    UiEngineImpl.pushModalScreen(Screen) line: 928    
    Dialog.doModal() line: 604    
    Dialog.alert(String) line: 436    
    WelcomeScreen(ServiceSelectionScreen).onUiEngineAttached(boolean) line: 108    
    WelcomeScreen.onUiEngineAttached(boolean) line: 42    
    WelcomeScreen(Screen).callOnUiEngineAttached(boolean) line: 438    
    UiEngineImpl.pushScreen(Screen) line: 632    
    MFSApplication(UiApplication).pushScreen(Screen) line: 292    
    MFSApplication.<init>() line: 202    
    MFSApplication.main(String[]) line: 72    

 

Here is the code in the error handler, as you can see i did do a lot of checks beforehand, but to no avail:

 

 

         UiApplication app = UiApplication.getUiApplication();
         if ((app.getActiveScreen() != null) && (app.isPaintingSuspended()==false) && (app.isAlive()) && 
               (app.isForeground()) && (app.getActiveScreen().isDisplayed()))
         {  
           UiApplication.getUiApplication().invokeLater(new ErrorDialog(e.getMessage()));
        }

 

 

Can soneone help me please!

Thanks,

Carl

-- Carl Eric Codère
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display Error (from different thread)

Best not to post on solved Threads, people don't often look at them.

 

But in this case your problem will probably be solved if you replace:

 

UiApplication.getUiApplication().invokeLater(new ErrorDialog(e.getMessage()));

with

 

 

UiApplication.getUiApplication().invokeLater(new Runnable() {

pubic void run() {

    new ErrorDialog(e.getMessage()));

}

});

 

I have to say new ErrorDialog(e.getMessage() looks a bit wierd, do you really need to create a new object?

 

New Developer
Posts: 17
Registered: ‎02-09-2010
My Device: Blackberry 9000
My Carrier: None

Re: Display Error (from different thread)

Greetings,

                Maybe i am too used in developing where creating classes inside a method feels strange. On the other hand, ErrorDialog implements the Runnable interface, so it should work in theory? Right?

 

p.s: Sorry for my late reply and thank you for your last reply!

Carl Eric Codere

 

-- Carl Eric Codère
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display Error (from different thread)

Yes.

 

I'm not used to a Dialog implementing Runnable, and presumed you were doing all your processing the constructor, though on reflection, that is a pretty silly thing to think....