qq_29500013 2016-04-27 07:54 采纳率: 90%
浏览 1696
已采纳

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条回答 默认 最新

  • 恋恋西风 2016-04-27 09:58
    关注

    1。安卓 有这个控件 Chronometer,你最好用这个;自己做,肯定要用到线程;
    2.我估计你是线程崩溃,引起的崩溃;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法