Problem with a ProgressDialog

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with a ProgressDialog

Postby jros » Mon Nov 09, 2009 8:46 am

Hi, I´m trying to show a ProgressDialog, this is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ProgressDialog mDialog;
  2.  
  3. protected void MostrarMensaje()
  4. {
  5.    
  6.     mDialog = ProgressDialog.show(this, "",
  7.             "Creando copia de seguridad. Por favor espere...", true);
  8.    
  9.     Thread thread = new Thread(new Runnable()
  10.     {
  11.        
  12.         public void run()
  13.         {
  14.             Sleep(10000);
  15.             handler.sendEmptyMessage(0);                
  16.         }
  17.     });
  18.     thread.start();
  19.    
  20. }
  21.  
  22. private Handler handler = new Handler() {
  23.     @Override
  24.     public void handleMessage(Message msg) {
  25.         mDialog.dismiss();
  26.     }
  27. };
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I get the next exception:

Code: Select all
11-08 23:29:33.295: ERROR/AndroidRuntime(15092): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()


Can you help me, please?

Thanks in advance.
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Top

Postby sandis84 » Mon Nov 09, 2009 8:56 am

Is your class really an activity?
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby jros » Mon Nov 09, 2009 9:14 am

Yes, of course.
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Postby sandis84 » Mon Nov 09, 2009 9:20 am

I know that I have got the same error once, when trying to do similar things with threading. However, I cant see anything wrong with your code. Perhaps you could try this: make your activity implement runnable, and thus implement the run-function directly in the activity.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby jros » Mon Nov 09, 2009 9:22 am

Thanks sandis84, I will try it asap.
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Postby jros » Mon Nov 09, 2009 11:57 am

I can´t believe!!

Now I´m at work, I don´t have the project here. I have open eclipse, I have create a sample application with an activity, I have copy/paste from here to the new activity and works fine ¿?¿? without implement the run-function directly in the activity.

Argh!!!

I don´t understand, when I arrive to home I will try another time and I will try to find what can to be the problem.

Thanks,
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Top

Resolved

Postby jros » Mon Nov 09, 2009 4:32 pm

Ok, sandis84, I have resolved my problem.

My real code in the thread was:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void run()
  2.  
  3. {
  4.  
  5.     Sleep(10000);
  6.  
  7.     Toast.makeText(MyClass.this, "End sleep", 1000).show();
  8.  
  9.     handler.sendEmptyMessage(0);                
  10.  
  11. }
  12.  
  13.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The exception is due to call to Toas.makeText.

Perhaps Toast, can´t show two dialogs from the same process or thread, I don´t know.

I have included my Toast.makeText call in the handler dispatcher:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Handler handler = new Handler() {
  2.  
  3.         @Override
  4.  
  5.         public void handleMessage(Message msg) {
  6.  
  7.                 mDialog.dismiss();
  8.  
  9.                 Toast.makeText(MyClass.this, "End sleep", 1000).show();
  10.  
  11.         }
  12.  
  13. };
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Thank you, anyway.
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Postby sandis84 » Tue Nov 10, 2009 12:39 pm

jros, only the activity-thread can write to the screen (like a toast). The other thread does not have these priviligies. That is why things like handlers exist: So other threads can send requests to the UI-thread.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby jros » Tue Nov 10, 2009 12:41 pm

Thank you very much Sandis, your comments are very appreciates by me.

Bye.
jros
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 1:52 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 5 guests