Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 线程运行中的问题_Android - Fatal编程技术网

Android 线程运行中的问题

Android 线程运行中的问题,android,Android,我用[start/pause]和[reset]按钮做简单的秒表。暂停后按下启动按钮时出现问题。run方法未调用。请帮帮我。 我的代码是 public class StopWatch3 extends Activity implements Runnable{ // text view influenced by the Thread private TextView threadModifiedText; int time=0; Button b1,b2,b3; boolean shouldR

我用[start/pause]和[reset]按钮做简单的秒表。暂停后按下启动按钮时出现问题。run方法未调用。请帮帮我。 我的代码是

public class StopWatch3 extends Activity implements Runnable{

// text view influenced by the Thread
private TextView threadModifiedText;
int time=0;
Button b1,b2,b3;
boolean shouldRun = false;
/** Called when the activity is first created. */
Thread currentThread = new Thread(this);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stopwatch);

    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    threadModifiedText = (TextView) findViewById(R.id.textView1);
    Log.e("before",""+currentThread.getState());
    b1.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            Log.e("stopw",(String) b1.getText());
            if(b1.getText().toString().equals("start")){
                    if(currentThread.getState()==Thread.State.NEW){
                        currentThread.start();
                        Log.e("after",""+currentThread.getState());
                        shouldRun = true;
                        b1.setText("pause");
                    }
                    else{
                      shouldRun = true;
                      b1.setText("pause");
                    }
            }
            else if(b1.getText().toString().equals("pause")){
                shouldRun = false;
                b1.setText("start");
            }
        }

    });     
    b2.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            time=0;
        }

    });               
}


@Override
public void run(){
    try {
        while(shouldRun){
            Thread.sleep(1000);
            threadHandler.sendEmptyMessage(0);
        }
    } catch (InterruptedException e) {
    }   
}


private Handler threadHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        time++;
        threadModifiedText.setText(""+time);
    }
};

}

我认为当您将shouldlrun设置为false时,您的线程就结束了。
将您的while循环包含到另一个while循环中,只要程序运行,该循环就是正确的。

在线程完成其任务后,您无法启动该线程,并且该线程的状态不再是新的。无论如何,在这种情况下,您必须创建一个新线程

当您第二次按“开始”时,您将到达此部分:

else{
    shouldRun = true;
    b1.setText("pause");
}
当然,这段代码中的任何内容都不会使线程再次运行