• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Threads in Android?

Bodom

Newbie
Jul 6, 2009
17
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:
public class Clock {
    
    
    private boolean isStarted = false;
    clockThread thread;
    TextView millisecondsView;
    
    
    public Clock(TextView millisecondsView) {
        this.millisecondsView = millisecondsView;
        
    }
    
    
    public void count() {
        
        if(!isStarted) {
            isStarted = true;
            thread = new clockThread();
            if(!thread.isAlive())
                thread.start();
        }
        else {
            isStarted = false;
            
            if(!thread.isAlive())
                thread.interrupt();
        }
            
    }
    
    private class clockThread extends Thread {
        @Override public void run() {
            millisecondsView.setText("SAD"); //here is the crash
            //the millisecondsView Object comes from the main Activity
        }
        
    }
        
    
}
 
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.
 
Upvote 0
Thanks for your answer.

No thats not the problem, the Obejct is initialised.

Here is the code from the main Activity:

Code:
package An.stop;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Anstop extends Activity {
    
    Clock clock;
    Button startButton;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView millisecondsView = (TextView) findViewById(R.id.millisecondsView);
        clock = new Clock(millisecondsView);
        
        startButton = (Button) findViewById(R.id.startButton);
        startButtonListener l = new startButtonListener(clock);
        startButton.setOnClickListener(l);
        
        //AccelerometerListener al = new AccelerometerListener(this, clock);
        //al.start();
        
    }
    
    
    private class startButtonListener implements OnClickListener {
        
        Clock clock;
        
        public startButtonListener(Clock clock) {
            this.clock = clock;
        }
        
        public void onClick(View v) {
          clock.count();
            
        }
    }

    
    
}
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:
private class clockThread extends Thread {
        @Override public void run() {
            try {
                millisecondsView.setText("SAD");
            }
            catch (Exception e) {
                Log.d("tag", e.getMessage());
            }
        }

But where can I see this message? ;D
 
Upvote 0
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.
 
Upvote 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?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones