问题描述
- android倒计时程序的崩溃问题
- 必须在分钟和秒两栏中都输入才可以正常计时,否则会崩溃,这是为什么??求大侠帮忙!!!
Java代码是
```package com.example.countt;
import java.util.Timer;
import java.util.TimerTask;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {
private EditText etMinute etSecond;
private Button btnStartTime btnStopTime;
private TextView time;
private int i = 0;
private int i2 = 0;
private Timer timer = null;
private TimerTask task = null;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews();}private void initViews() { etMinute = (EditText) findViewById(R.id.etMinute); etSecond = (EditText) findViewById(R.id.etSecond); btnStartTime = (Button) findViewById(R.id.btnStartTime); btnStopTime = (Button) findViewById(R.id.stopTime); time = (TextView) findViewById(R.id.time); btnStartTime.setOnClickListener(this); btnStopTime.setOnClickListener(this);}@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.btnStartTime: i = Integer.parseInt(etSecond.getText().toString()); i2 = Integer.parseInt(etMinute.getText().toString()); startTime(); break; case R.id.stopTime: stoptTime(); break; default: break; }}private Handler sHandler = new Handler() { public void handleMessage(Message msg) { if (msg.arg1 == -1) { timer.cancel(); } else if(msg.arg1==0){ time.setText(""00:00""); } else if (msg.arg1 != 0 && ((int) msg.arg1/60) < 10 && msg.arg1%60 < 10) { time.setText(""0"" + (int) msg.arg1 / 60 + "":"" + ""0"" + msg.arg1 % 60); startTime(); } else if (msg.arg1 != 0 && ((int) msg.arg1/60) > 10 && msg.arg1%60 < 10) { time.setText((int) msg.arg1 / 60 + "":"" + ""0"" + msg.arg1 % 60); startTime(); } else if (msg.arg1 != 0 && ((int) msg.arg1/60) < 10 && msg.arg1%60 > 10) { time.setText(""0"" + (int) msg.arg1 / 60 + "":"" + msg.arg1 % 60); startTime(); } else { time.setText((int) msg.arg1 / 60 + "":"" + msg.arg1 % 60); startTime(); } };};private void startTime() { timer = new Timer(); task = new TimerTask() { @Override public void run() { if(i==0&&i2==0){ timer.cancel(); } if(i2==0&&i!=0){ Message msg = sHandler.obtainMessage(); msg.arg1 = i; sHandler.sendMessage(msg); } else if (i == 0&&i2!=0) { i2--; i = 60; Message msg = sHandler.obtainMessage(); msg.arg1 = i + i2 * 60; sHandler.sendMessage(msg); } else { i--; Message msg = sHandler.obtainMessage(); msg.arg1 = i + i2 * 60; sHandler.sendMessage(msg); } } }; timer.schedule(task 1000);}private void stoptTime() { timer.cancel();}
}
layout布局文件是```<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android"" xmlns:tools=""http://schemas.android.com/tools"" android:layout_width=""match_parent"" android:layout_height=""match_parent"" android:orientation=""vertical"" tools:context=""com.example.countt.MainActivity"" ><LinearLayout android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:orientation=""horizontal""> <EditText android:id=""@+id/etMinute"" android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:layout_weight=""1"" > </EditText> <TextView android:layout_width=""wrap_content"" android:layout_height=""wrap_content"" android:text=""分钟 ""/> <EditText android:id=""@+id/etSecond"" android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:layout_weight=""1"" > </EditText> <TextView android:layout_width=""wrap_content"" android:layout_height=""wrap_content"" android:text=""秒 ""/></LinearLayout><LinearLayout android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:gravity=""center"" android:orientation=""horizontal""> <TextView android:gravity=""center"" android:id=""@+id/time"" android:layout_width=""match_parent"" android:layout_height=""300dp"" android:text=""00:00"" android:textColor=""#0000ff"" android:textSize=""40sp"" /> </LinearLayout> <Button android:id=""@+id/btnStartTime"" android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:text=""开始计时"" /> <Button android:id=""@+id/stopTime"" android:layout_width=""match_parent"" android:layout_height=""wrap_content"" android:text=""停止计时"" /></LinearLayout>
图
解决方案
1。安卓 有这个控件 Chronometer,你最好用这个;自己做,肯定要用到线程;
2.我估计你是线程崩溃,引起的崩溃;
解决方案二:
不嫌弃的话可以试试我这个,不过没什么注释
public class CustomTimer {
private Handler mHandler;
private static final int TIME_UNIT = 1000;
private static final int RADIX = 60;
private int totalSec = 0;
private Timer mTimer;
private TimerTask mTask;
public static final int MSG_WHAT = 1111;public static final int MSG_PAUSE = 1112;public static final int MSG_CONTINUE = 1113;public CustomTimer(final Handler handler) { this.mHandler = handler; mTimer = new Timer(true);}/** * 倒计时秒数 * * @param max */public void countDownTime(final int max) { if (mTimer == null) mTimer = new Timer(true); mTask = new TimerTask() { @Override public void run() { Message mMessage = mHandler.obtainMessage(); mMessage.what = MSG_WHAT; totalSec++; mMessage.obj = max - totalSec; mHandler.sendMessage(mMessage); } }; mTimer.schedule(mTask TIME_UNIT TIME_UNIT);}/** * 倒计时时分秒 * * @param max */public void countDownFormatTime(final int max) { if (mTimer == null) mTimer = new Timer(true); mTask = new TimerTask() { @Override public void run() { Message mMessage = mHandler.obtainMessage(); mMessage.what = MSG_WHAT; totalSec++; mMessage.obj = getFormatTime(max - totalSec); mHandler.sendMessage(mMessage); } }; mTimer.schedule(mTask TIME_UNIT TIME_UNIT);}public void countTime(int start) { if (mTimer == null) mTimer = new Timer(true); totalSec = start; mTask = new TimerTask() { @Override public void run() { totalSec++; Message mMessage = mHandler.obtainMessage(); mMessage.what = MSG_WHAT; mMessage.obj = getFormatTime(totalSec); mHandler.sendMessage(mMessage); } }; mTimer.schedule(mTask TIME_UNIT TIME_UNIT);}public int stopCountDown() { int usedTime = totalSec; if (mTask != null) { mTask.cancel(); totalSec = 0; mTask = null; mTimer = null; } return usedTime;}public String stopCountDownFormatTime() { String usedTime = getFormatTime(totalSec); if (mTask != null) { mTask.cancel(); totalSec = 0; mTask = null; mTimer = null; } return usedTime;}public String stopCount() { String usedTime = getFormatTime(totalSec); if (mTask != null) { mTask.cancel(); mTask = null; mTimer = null; totalSec = 0; } return usedTime;}public static String getFormatTime(int totalSec) { int minute = totalSec / RADIX; int second = totalSec % RADIX; return String.format(""%1$02d:%2$02d"" minute second);}public static int getNotFormatTime(String time) { int ret = 0; if (time != null) { String[] split = time.split("":""); ret = Integer.valueOf(split[0]) * 60 + Integer.valueOf(split[1]); } return ret;}
}
这是一个简单的倒计时例子
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case CustomTimer.MSG_WHAT: //计时器发来的信息
String time = msg.obj.toString();
txt_time.setText(time); //计时操作
//考试时间到
if (time.equals(""00:00"")) {
txt_time.setText(""00:00"");
mTimer.stopCount();//停止计时操作
} break; } } }; mTimer = new CustomTimer(mHandler);//初始化计时器 mTimer.countDownFormatTime(100); 另外,还有正计时的功能。
解决方案三:
public class CustomTimer {
private Handler mHandler;
private static final int TIME_UNIT = 1000;
private static final int RADIX = 60;
private int totalSec = 0;
private Timer mTimer;
private TimerTask mTask;
public static final int MSG_WHAT = 1111;
public static final int MSG_PAUSE = 1112;
public static final int MSG_CONTINUE = 1113;
public CustomTimer(final Handler handler) {
this.mHandler = handler;
mTimer = new Timer(true);
}
/**
- 倒计时秒数*
- @param max
*/
public void countDownTime(final int max) {
if (mTimer == null)
mTimer = new Timer(true);mTask = new TimerTask() {
@Override
public void run() {
Message mMessage = mHandler.obtainMessage();
mMessage.what = MSG_WHAT;
totalSec++;
mMessage.obj = max - totalSec;
mHandler.sendMessage(mMessage);
}
};
mTimer.schedule(mTask TIME_UNIT TIME_UNIT);
}
/**
- 倒计时时分秒*
- @param max
*/
public void countDownFormatTime(final int max) {
if (mTimer == null)
mTimer = new Timer(true);mTask = new TimerTask() {
@Override
public void run() {
Message mMessage = mHandler.obtainMessage();
mMessage.what = MSG_WHAT;
totalSec++;
mMessage.obj = getFormatTime(max - totalSec);
mHandler.sendMessage(mMessage);
}
};
mTimer.schedule(mTask TIME_UNIT TIME_UNIT);
}
public void countTime(int start) {
if (mTimer == null)
mTimer = new Timer(true);
totalSec = start;
mTask = new TimerTask() {
@Override
public void run() {
totalSec++;
Message mMessage = mHandler.obtainMessage();
mMessage.what = MSG_WHAT;
mMessage.obj = getFormatTime(totalSec);
mHandler.sendMessage(mMessage);
}
};
mTimer.schedule(mTask TIME_UNIT TIME_UNIT);
}
public int stopCountDown() {
int usedTime = totalSec;
if (mTask != null) {
mTask.cancel();
totalSec = 0;
mTask = null;
mTimer = null;
}
return usedTime;
}
public String stopCountDownFormatTime() {
String usedTime = getFormatTime(totalSec);
if (mTask != null) {
mTask.cancel();
totalSec = 0;
mTask = null;
mTimer = null;
}
return usedTime;
}
public String stopCount() {
String usedTime = getFormatTime(totalSec);
if (mTask != null) {
mTask.cancel();
mTask = null;
mTimer = null;
totalSec = 0;
}
return usedTime;
}
public static String getFormatTime(int totalSec) {
int minute = totalSec / RADIX;
int second = totalSec % RADIX;
return String.format(""%1$02d:%2$02d"" minute second);
}
public static int getNotFormatTime(String time) {
int ret = 0;
if (time != null) {
String[] split = time.split("":"");
ret = Integer.valueOf(split[0]) * 60 + Integer.valueOf(split[1]);
}
return ret;
}
}
这是一个简单的倒计时例子
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case CustomTimer.MSG_WHAT: //计时器发来的信息
String time = msg.obj.toString();
txt_time.setText(time); //计时操作
//考试时间到
if (time.equals(""00:00"")) {
txt_time.setText(""00:00"");
mTimer.stopCount();//停止计时操作
} break; } }};mTimer = new CustomTimer(mHandler);//初始化计时器 mTimer.countDownFormatTime(100); 另外,还有正计时的功能。
解决方案四:
你可以给时分都设置一个默认值呀