Threads in Android?


Last Updated:

  1. Bodom

    Bodom Member This Topic's Starter

    Joined:
    Jul 6, 2009
    Messages:
    17
    Likes Received:
    0
    Hello,

    at first sorry for my bad Englisch, I am coming form Germany ;D.

    Okay, i have a problem with a Thread in Android, I want to make a stopping watch. So my first try was to make a thread which refreshs a TextView every second. But when i call TextView.setText("str"), the program always crashes. I really don't know why, so please help me. Thank you

    The Code:

    import android.widget.TextView;

    Code (Text):
    1. public class Clock {
    2.    
    3.    
    4.     private boolean isStarted = false;
    5.     clockThread thread;
    6.     TextView millisecondsView;
    7.    
    8.    
    9.     public Clock(TextView millisecondsView) {
    10.         this.millisecondsView = millisecondsView;
    11.        
    12.     }
    13.    
    14.    
    15.     public void count() {
    16.        
    17.         if(!isStarted) {
    18.             isStarted = true;
    19.             thread = new clockThread();
    20.             if(!thread.isAlive())
    21.                 thread.start();
    22.         }
    23.         else {
    24.             isStarted = false;
    25.            
    26.             if(!thread.isAlive())
    27.                 thread.interrupt();
    28.         }
    29.            
    30.     }
    31.    
    32.     private class clockThread extends Thread {
    33.         @Override public void run() {
    34.             millisecondsView.setText("SAD"); //here is the crash
    35.             //the millisecondsView Object comes from the main Activity
    36.         }
    37.        
    38.     }
    39.        
    40.    
    41. }
     

    Advertisement
  2. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    Umm, what is the state of your millisecondsView object?

    Perhaps you left out some code, but you need to call "findViewById()" or something similar prior to calling "millisecondsView.setText()". As it is, it's likely you're working with an uninitialised object, which is why it fails.

    If all else fails, wrap it in a try-catch and print (with "Log.d()") the exception's message, to see what kind of problem you're dealing with.
     
  3. Bodom

    Bodom Member This Topic's Starter

    Joined:
    Jul 6, 2009
    Messages:
    17
    Likes Received:
    0
    Thanks for your answer.

    No thats not the problem, the Obejct is initialised.

    Here is the code from the main Activity:

    Code (Text):
    1. package An.stop;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. import android.widget.TextView;
    9.  
    10. public class Anstop extends Activity {
    11.    
    12.     Clock clock;
    13.     Button startButton;
    14.    
    15.     /** Called when the activity is first created. */
    16.     @Override
    17.     public void onCreate(Bundle savedInstanceState) {
    18.         super.onCreate(savedInstanceState);
    19.         setContentView(R.layout.main);
    20.        
    21.         TextView millisecondsView = (TextView) findViewById(R.id.millisecondsView);
    22.         clock = new Clock(millisecondsView);
    23.        
    24.         startButton = (Button) findViewById(R.id.startButton);
    25.         startButtonListener l = new startButtonListener(clock);
    26.         startButton.setOnClickListener(l);
    27.        
    28.         //AccelerometerListener al = new AccelerometerListener(this, clock);
    29.         //al.start();
    30.        
    31.     }
    32.    
    33.    
    34.     private class startButtonListener implements OnClickListener {
    35.        
    36.         Clock clock;
    37.        
    38.         public startButtonListener(Clock clock) {
    39.             this.clock = clock;
    40.         }
    41.        
    42.         public void onClick(View v) {
    43.           clock.count();
    44.            
    45.         }
    46.     }
    47.  
    48.    
    49.    
    50. }
    So the Object is given to the constructor of the other class. I also tried to make a method in the Anstop class, which sets the Text of the TextView, but this ends also in a crash.

    I tried to log the Error Message, my Code is now this:

    Code (Text):
    1. private class clockThread extends Thread {
    2.         @Override public void run() {
    3.             try {
    4.                 millisecondsView.setText("SAD");
    5.             }
    6.             catch (Exception e) {
    7.                 Log.d("tag", e.getMessage());
    8.             }
    9.         }
    But where can I see this message? ;D
     
  4. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    Heh, you need to run a debugger alongside your sim.

    There's one tucked away somewhere in the Android folder tree, or you can see the debug output in a console within Eclipse (you can't have both at the same time).

    I use the Android tool, because it's very easy to set up filters -- that what the "tag" string is for, so you can label it anything (say, "anstop") and filter the debug log so you only see 'your' messages and not the entire torrent.
     
  5. Bodom

    Bodom Member This Topic's Starter

    Joined:
    Jul 6, 2009
    Messages:
    17
    Likes Received:
    0
    Okay I found it.
    The Error is:

    Only the original thread that created a view hierachy can touch its view.


    Hmm thats a problemn xD, anyone know to solve this problem? I dont know how I can make a stopping watch without a count thread :D

    Or asked in another way, how can I inform the main Thread\Activity to refresh the TextView?
     
  6. Bodom

    Bodom Member This Topic's Starter

    Joined:
    Jul 6, 2009
    Messages:
    17
    Likes Received:
    0
  7. KlaymenDK

    KlaymenDK Well-Known Member

    Joined:
    May 29, 2009
    Messages:
    1,217
    Likes Received:
    130
    Well there you go. That'll teach you to catch exceptions so you can tell what's really going on. :p

    I'm glad it worked out for you. I'm going to unsubscribe from this thread, so I probably won't see any further updates you make. Cheers!
     

Share This Page

Loading...