我一直在我的应用程序上编写一个计时器,这样当你点击一个按钮时,文本会显示一个以 0.1 秒为增量计数的时间。
我虽然编码正确,但事实证明,每当我按下手机上的就绪按钮时,应用程序就会崩溃......有人可以给我一个提示,说明出了什么问题。我认为这可能与启动计时器有关?
我的 OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_double_player);
Button readyBtn = (Button) findViewById(R.id.readyBtn);
readyBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
Button readyBtn = (Button) findViewById(R.id.readyBtn);
Spinner selectorLeft = (Spinner) findViewById(R.id.selectorLeft);
Spinner selectorRight = (Spinner) findViewById(R.id.selectorRight);
ImageView narLeft = (ImageView) findViewById(R.id.narwhalleft);
ImageView narRight = (ImageView) findViewById(R.id.narwhalRight);
selectorLeft.setVisibility(RelativeLayout.GONE);
selectorRight.setVisibility(RelativeLayout.GONE);
readyBtn.setVisibility(RelativeLayout.GONE);
final TextView text = (TextView) findViewById(R.id.timerText);
final Handler handler;
text.setText(0);
handler = new Handler();
Runnable runnable = new Runnable(){
boolean ready = true;
int number = 0;
@Override
public void run(){
while(ready){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
handler.post(new Runnable(){
@Override
public void run(){
number+=0.1;
text.setText(String.valueOf(number));
}
});
}
}
};
new Thread(runnable).start();
narRight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
int clicksNeeded = 100;
boolean rightWon = false;
int clicksClickedRight = 0;
clicksClickedRight = clicksClickedRight + 1;
if(clicksClickedRight == clicksNeeded){
rightWon = true;
}
if(rightWon == true){
}
}
});
narLeft.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
int clicksNeeded = 100;
boolean leftWon = false;
int clicksClickedLeft = 0;
clicksClickedLeft = clicksClickedLeft + 1;
if(clicksClickedLeft == clicksNeeded){
leftWon = true;
}
if(leftWon == true){
}
}
});
}
});
}