Problem with Thread.Sleep()

Put your problem here if it does not fit any of the other categories.

Problem with Thread.Sleep()

Postby Mag1c » Tue Jan 18, 2011 6:02 pm

Hey,
Im VERY new into Android coding and I have an ImageButton that I want him to change the image after 2 seconds, here is the code:
Code: Select all
red.setImageResource(R.drawable.red);                 
                 
                
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

red.setImageResource(R.drawable.blue);


as you can see, it changes to an image with red color, and then after 2 seconds, it changes for a blue image..
Why isnt this happening when I run it? I mean I dont see it changing the image..
Thanks.
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Top

Re: Problem with Thread.Sleep()

Postby jonbonazza » Wed Jan 19, 2011 8:03 pm

You are attempting to make UI changes in a thread other than the UI thread. You need to implement a Handler to post back to the UI thread and make changes.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Problem with Thread.Sleep()

Postby Mag1c » Wed Jan 19, 2011 11:55 pm

Thanks for the reply, but I'm sorry to ask this but can you please explain more on how to fix this?
The command "red.setImageResource(R.drawable.blue);" works fine and it does change the color of the button. So whats the problem? how can I stop for 2 seconds between the color changing
Thanks again
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Re: Problem with Thread.Sleep()

Postby jonbonazza » Thu Jan 20, 2011 2:00 am

Ah I am sorry, I misunderstood your question. At any rate, the best thing to do would be to use a Timer.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Problem with Thread.Sleep()

Postby Mag1c » Sun Jan 23, 2011 4:09 pm

jonbonazza wrote:Ah I am sorry, I misunderstood your question. At any rate, the best thing to do would be to use a Timer.


Hey,
Can you please further explain how can a Timer stop the changing for a few seconds?
Or can I send to the timer also the background i wanna change to and they change there?
Thanks.
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Re: Problem with Thread.Sleep()

Postby jonbonazza » Sun Jan 23, 2011 8:37 pm

Well, you create a subclass of TimerTask, which is essentially a delayed thread. in the overridden run() method of this class, you add the code you want to be fired after 2 seconds. Lastly, you create a Timer object and then use the start() method, sending it a reference to the TimerTask subclass you created, along with the delay in your case, 2000 ms).

Look here:
http://developer.android.com/reference/ ... Timer.html

and here
http://developer.android.com/reference/ ... rTask.html
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Re: Problem with Thread.Sleep()

Postby Mag1c » Sun Jan 30, 2011 1:36 pm

Thanks again,
I've tried to make a really simple timer where it adds 1 every 2 seconds in an EditText:
Code: Select all
public class timeractivity extends Activity {
   Timer timer;
   Button b1;
   public  EditText edittext;
   int x = 0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        b1 = (Button)findViewById(R.id.Button01);
        edittext = (EditText)findViewById(R.id.EditText01);
       
        b1.setOnClickListener(new View.OnClickListener() {         
         @Override
         public void onClick(View v) {
             timer.schedule(new RemindTask(), 2000);
         }
      });           

       
       
    }
    class RemindTask extends TimerTask {
        public void run() {           
           x+=1;
            edittext.setText(x);
        }
   
}
}


But when I click on the button I get FC..
Why is this happening?
Sorry for all those questions but I really want to learn coding.
Mag1c
Developer
Developer
 
Posts: 28
Joined: Tue Jan 04, 2011 12:15 am

Re: Problem with Thread.Sleep()

Postby jonbonazza » Thu Mar 10, 2011 2:59 am

Well, TimerTask is essentially a thread, so like any other thread, when attempting to make changes to the UI, you need to use a Handler to post back to the UI thread.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class timeractivity extends Activity {
  2.    Timer timer;
  3.    Button b1;
  4.    public  EditText edittext;
  5.    int x = 0;
  6.    
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.main);
  12.        
  13.         b1 = (Button)findViewById(R.id.Button01);
  14.         edittext = (EditText)findViewById(R.id.EditText01);
  15.        
  16.         b1.setOnClickListener(new View.OnClickListener() {        
  17.          @Override
  18.          public void onClick(View v) {
  19.              timer.schedule(new RemindTask(), 2000);
  20.          }
  21.       });          
  22.  
  23.        
  24.        
  25.     }
  26.     class RemindTask extends TimerTask {
  27.         public void run() {          
  28.            mHandler.sendEmptyMessage(0);
  29.         }  
  30.  
  31.  
  32. }
  33. Handler mHandler = new Handler()
  34. {
  35.       @Override
  36.       public void handleMessage(Message msg)
  37.       {
  38.             x+=1;
  39.             edittext.setText(x);
  40.       }
  41. };
  42. }
  43.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Problem with Thread.Sleep()

Postby raj_J2ME » Thu Mar 10, 2011 6:55 am

But when I click on the button I get FC..
Why is this happening?
Sorry for all those questions but I really want to learn coding.

What do you mean FC, you are getting?

I am not able to see that where did you create the instance of the Timer's object timer,
You have to initialize this object before you can schedule this.
Using,
timer = new Timer();
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Problem with Thread.Sleep()

Postby vhalexxs » Wed Mar 16, 2011 6:42 am

You can use Handler to change the image after 2 seconds. See the source code below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.main);
  4.        
  5.         Button submit = (Button) findViewById(R.id.submit);
  6.         submit.setOnClickListener(new View.OnClickListener() {
  7.                         @Override
  8.                         public void onClick(View v) {
  9.                                 handler.sendMessageDelayed(handler.obtainMessage(), 2000);
  10.                         }
  11.                 });
  12.     }
  13.    
  14.     private Handler handler = new Handler() {
  15.         @Override
  16.         public void handleMessage(Message msg) {
  17.                 ImageView image1 = (ImageView) findViewById(R.id.image1);
  18.                 image1.setImageResource(R.drawable.images);
  19.         }
  20.     };
  21.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Best,
Alex Escobar
Senior Android Engineer
-----------------------
Airpush.com: The Push Notification Ad Network
User avatar
vhalexxs
Developer
Developer
 
Posts: 28
Joined: Mon Mar 14, 2011 3:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 30 guests