简体   繁体   English

为什么我的线程不起作用?

[英]Why my thread doesn't work?

I make an app that can count down. 我制作的应用程序可以倒计时。 But it doesn't work and it just shows 100 in textview. 但是它不起作用,它在textview中仅显示100。

Here is my code: 这是我的代码:

public class MainActivity extends Activity {

    private TextView textView;
    private Button start;
    Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView1);
        start = (Button) findViewById(R.id.button1);
        start.setOnClickListener(onStart);
        thread = new Thread( //it's my thread
                new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 99; i > 0; i--) {
                            Log.i("Where am I?", "I'm in for loop .");
                            try {
                                textView.setText(String.valueOf(i));
                            } catch (Exception e) {
                                Log.e("Exception.getCause", String.valueOf(e.getCause()), e.getCause());
                            }
                            Log.i("INDEX", String.valueOf(i));
                        }
                    }
                });
    }

    private View.OnClickListener onStart = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Where am I?", "I in View.OnClickListener .");
            thread.start();
        }
    };
}

Update your TextView using runOnUiThread as below... 如下所示使用runOnUiThread更新您的TextView ...

runOnUiThread(new Runnable() {
   @Override    
   public void run() {

       textView.setText(String.valueOf(i));

   }
});

Update: 更新:

For delay in count you can use Handler as below. 对于计数延迟,可以使用以下Handler Create an object of Handler and create a Thread . 创建一个Handler对象并创建一个Thread

private Handler mTimerHandler = new Handler();

private Runnable mTimerExecutor = new Runnable() {

    @Override
    public void run() {

         //add your code here which should execute after the specified delay

    }
};

Then pass that thread inside postDelayed() method Handler with the time which should be delayed to execute the thread as below... 然后将该线程传递到postDelayed()方法Handler ,并延迟执行该线程的时间,如下所示...

mTimerHandler.postDelayed(mTimerExecutor, 1000);

And if you want to cancel the thread to execute, use removeCallbacks() method as below... 如果要取消执行线程,请使用removeCallbacks()方法,如下所示...

mTimerHandler.removeCallbacks(mTimerExecutor);

Catching for Exception inside the Thread is kind of misleading. 在线程内捕获Exception是一种误导。 As matter of fact, textView.setText(String.valueOf(i)); 事实上, textView.setText(String.valueOf(i)); executed in a Thread different from the UI Thread should make you app crashes for CalledFromTheWrongThreadException . 在与UI Thread Thread不同的UI Thread应该会使您的CalledFromTheWrongThreadException应用程序崩溃。 You should use an Handler to execute that line in the UI Thread's context 您应该使用Handler在UI线程的上下文中执行该行

textView.setText(String.valueOf(i));

必须仅在UI线程中使用。

textView.setText(String.valueOf(i)); 

This is UI action, You can handle UI action only in a main thread. 这是UI操作,您只能在主线程中处理UI操作。

You can send a message to the handle of activity. 您可以将消息发送到活动句柄。

You cannot use Thread class to interact with UI you should use AsyncTask or Handler classes. 您不能使用Thread类与UI交互,而应使用AsyncTask或Handler类。 Sample tutorial: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html 示例教程: http : //www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM