[英]Android Countdown Timer app crashes when the button is clicked
I've just started coding in Android SDK and this is my first attempt at a Countdown Timer. 我刚刚开始使用Android SDK进行编码,这是我首次尝试使用倒数计时器。 I've set the timer to start at the press of the button with setOnClickListener().
我使用setOnClickListener()将计时器设置为在按下按钮时开始。 The timer doesn't start but the app stops working when I click the button.
计时器没有启动,但是当我单击按钮时,应用程序停止运行。 The device displays a message saying the app has stopped working.
设备显示一条消息,说明该应用程序已停止运行。
I've written a method to format the time into minutes and seconds. 我已经编写了一种将时间格式化为分钟和秒的方法。 And i've set the call in the onTick() method of the CountDownTimer.
我已经在CountDownTimer的onTick()方法中设置了调用。
I've posted my code below. 我已经在下面发布了我的代码。 Please take a look and point me to where the problem is and what to do.
请看一看,并指出问题所在和解决方法。 Much appreciated.
非常感激。
package com.example.day1;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView Timer;//Textview to display the timer
//This is the method to format the time into minutes and seconds
public String formatTime(long millis)
{
String output="00:00";
long second=millis/1000;
long minute=second/60;
second=second%60;
minute=minute%60;
String sec=String.valueOf(second);
String min=String.valueOf(minute);
output=min+":"+sec;
return output;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countdown);
Timer=(TextView) findViewById(R.id.text1);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(60000,1000)
{
public void onTick(long millisUntilFinished){
Timer.setText(formatTime(millisUntilFinished));
}
public void onFinish()
{
onStop();
}
}.start();
}
});
}
}
(1) Your app crash because you do not implements your activity with OnClickListener: you need to extend the method of your activity onclicklistener evet (1)您的应用程序崩溃,因为您未使用OnClickListener实现活动:您需要扩展活动方法onclicklistener evet
Here is the code 这是代码
package com.example.day1;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
///////////////////The solution is fix here///////////////
public class MainActivity extends Activity implements View.OnClickListener {
///////////////////End of solution//////////
TextView Timer;//Textview to display the timer
//This is the method to format the time into minutes and seconds
public String formatTime(long millis)
{
String output="00:00";
long second=millis/1000;
long minute=second/60;
second=second%60;
minute=minute%60;
String sec=String.valueOf(second);
String min=String.valueOf(minute);
output=min+":"+sec;
return output;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countdown);
Timer=(TextView) findViewById(R.id.text1);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(60000,1000)
{
public void onTick(long millisUntilFinished){
Timer.setText(formatTime(millisUntilFinished));
}
public void onFinish()
{
onStop();
}
}.start();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.