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;

@Override
protected 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);
}

@Override
public 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>


图片说明

4个回答

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);

        另外,还有正计时的功能。

你可以给时分都设置一个默认值呀

qq_29500013
qq_29500013 回复东风玖哥: 试试
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Android 倒计时程序
Android 倒计时程序,简单易懂!欢迎大家下载~
Android 倒计时程序源码
android倒计时 功能较为全面。
android倒计时程序
好几种样式的倒计时程序,android初学者可以随意更改。
android 裁剪图片完 程序会崩溃问题
之前在开发过程中使用手机自带的图片裁剪器对图片进行裁剪,裁剪完后程序会崩溃。 intent中有个参数不要设置就是”return-data” 不然配置不高的手机就会出现这种问题Intent intent = new Intent(“com.android.camera.action.CROP”);//调用Android系统自带的一个图片剪裁页面, intent.
Android 虚拟机崩溃的问题
虚拟机运行起来会崩溃,什么原因?说是0xXXXXXX内存不能为read。
Android混淆崩溃问题
Android混淆崩溃问题一、原始崩溃日志二、还原后的崩溃日志三、源代码四、解决方案五、参考文章六、Finally 上周四,导出『神马笔记』发布包的时候,遇到混淆后NullPointerException问题。 一、原始崩溃日志 java.lang.NullPointerException: throw with null exception at app.haiyunshan.whatsnot...
Android倒计时 Android仿京东倒计时 Android商城倒计时
Android倒计时 Android仿京东倒计时 Android商城倒计时 博客地址 http://blog.csdn.net/yu_blog/article/details/70257211
倒计时程序的问题
import javax.swing.*;rnimport java.awt.*;rnimport java.awt.event.*;rnrnpublic class countdownrnrn JFrame frm;rn JPanel p;rn JTextField txthour,txtmin,txtsec;rn JButton btnOK;rn rn public countdown()rn rn frm= new JFrame("倒计时");rn p = new JPanel();rn btnOK = new JButton("开始");rn Mylst my = new Mylst();rn btnOK.addActionListener(my);rn txthour = new JTextField(3);rn txtmin = new JTextField(3);rn txtsec = new JTextField(3);rn p.add(txthour);rn p.add(txtmin);rn p.add(txtsec);rn p.add(btnOK);rn rn rn rn rn rn frm.getContentPane().add(p);rn frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn frm.setSize(300,300);rn frm.setVisible(true);rnrn rn rn public static void main(String agrs[])rn rn new countdown();rnrn rn class Mylst implements ActionListenerrn rn public void actionPerformed(ActionEvent evt)rn rn oneth one = new oneth();rn one.start();rn rn rn rn class oneth extends Threadrn rn rn public void run()rn rn String sec = txtsec.getText();rn int se=Integer.parseInt(sec);rn String min=txtmin.getText();rn int mi=Integer.parseInt(min);rn String hor = txthour.getText();rn int hr = Integer.parseInt(hor);rn rn int secleft = 59 - se;rn int minleft = 59 - mi;rn int hourleft = 23 - hr;rn rnrnrnrn if(secleft<0)rn rn secleft = 60+ secleft;rn minleft = minleft -1;rn rn if(minleft<0)rn rn minleft = 60 + minleft;rn hourleft = hourleft - 1;rn rn if(hourleft<0)rn rn return;rn rn rn tryrn rn sleep(1000);rn rn catch(InterruptedException e)rn rn System.out.println("错误");rn rn rn rn rnrn本人刚刚接触java,遇到好多不懂的问题!!rn最近学拉线程,我编拉一个倒计时的程序,可是rn输入秒,分,时,后不会执行~!请问,我这个程序存在什么问题
程序debug不崩溃release崩溃的问题
敢问技术去的大神们,有没有遇到用vs2010写的c++程序,debug调试运行据正常,release下F5起来也正常,但是,单独运行release下的exe却出现程序崩溃问题?一般是什么会是原因?谢谢了[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/6.gif][/img][img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/2.gif][/img]
Android 倒计时
Android 倒计时效果
android倒计时
一、自定义倒计时控件——TimerTextView 显然TimerTextView应该派生于TextView,因为他本就是显示一串Text,只是具有了自动更新的功能,这里的自动更新的实现当然只通过线程来实现了,所以要继承Runnable接口。所以它的定义应该是这样的: [java] view plaincopy public class T
倒计时(android)
我们在进行做跳转界面的时候经常会遇到几秒后跳转,如下图:   那么,今天就简单给大家介绍一下倒计时的使用: public class MainActivity extends Activity {
Android倒计时
使用Handler的一个倒计时,支持单个计时。
android 倒计时
本例子是一个用android实现的倒计时!!!
android 倒计时
此demo可以实现倒计时功能,类似于短信认证功能所需要的倒计时一样。 注:注释掉的代码也可以实现倒计时,但是只能实现一次倒计时
android 下的倒计时
new CountDownTimer(10000,1000) { @Override public void onTick(long millisUntilFinished) { Log.e(TAG, "还剩下的秒数: "+millisUntilFinished/1000 ); } @Override public void onFini
相关热词 c# 线程结束时执行 c# kb mb 图片 c# 替换第几位字符 c#项目决定成败 c# 与matlab c# xml缩进 c#传感器基础 c#操作wps c# md5使用方法 c# 引用父窗口的组件