代码仅在有条件的情况下有效?
当我按下按钮一次时,while循环停止,并显示消息,但是当我再次按下按钮时,while循环将不会再次启动(我知道这一点是因为可运行中的消息没有显示)。
另外,线程中的 while(!boo) 和 boo=true 的组合;按钮中不会产生任何结果。
我可能做错了什么?我把布尔值 boo=true;在 onCreate 之外,所以我认为这不是问题......
public class UiTester extends Activity {
protected static final String TAG = null;
String s="";
Button stopper;
TextView display3;
//Boolean boo=true;
int n=0;
public Boolean boo=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
on=(Button) findViewById(R.id.bon);
off=(Button) findViewById(R.id.boff);
display=(TextView) findViewById(R.id.tvdisplay);
display3=(TextView) findViewById(R.id.tvdisplay3);
stopper=(Button) findViewById(R.id.stops);
final Handler handler = new Handler();
final Runnable updater = new Runnable() {
public void run() {
n++;
display3.setText("System On"+n);
}
};
stopper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(boo==true)
{
boo=false;
display3.setText("System Off");
}
else{
boo=true;
}
}
});
Thread x = new Thread() {
public void run() {
while (boo) {
handler.post(updater);
//non UI elements can go here
try {
Log.d(TAG, "local Thread sleeping");
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(TAG, "local Thread error", e);
}
}
}
};
x.start();
}
}
When I press the button once, the while loop stops, and the message is displayed, but when I press it again, the while loop will not start again (I know this because the message in the runnable is not displayed).
Also, the combination while(!boo) in the thread and a boo=true; in the button does not produce any result.
What might I be doing wrong? I put Boolean boo=true; outside onCreate, so I don't think that is the problem...
public class UiTester extends Activity {
protected static final String TAG = null;
String s="";
Button stopper;
TextView display3;
//Boolean boo=true;
int n=0;
public Boolean boo=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
on=(Button) findViewById(R.id.bon);
off=(Button) findViewById(R.id.boff);
display=(TextView) findViewById(R.id.tvdisplay);
display3=(TextView) findViewById(R.id.tvdisplay3);
stopper=(Button) findViewById(R.id.stops);
final Handler handler = new Handler();
final Runnable updater = new Runnable() {
public void run() {
n++;
display3.setText("System On"+n);
}
};
stopper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(boo==true)
{
boo=false;
display3.setText("System Off");
}
else{
boo=true;
}
}
});
Thread x = new Thread() {
public void run() {
while (boo) {
handler.post(updater);
//non UI elements can go here
try {
Log.d(TAG, "local Thread sleeping");
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(TAG, "local Thread error", e);
}
}
}
};
x.start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 boo 为 false 时,您的线程就会结束。它不会重新开始。
When boo is false your thread just ends. It will not start over again.