程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> timertask-android倒計時程序的崩潰問題

timertask-android倒計時程序的崩潰問題

編輯:編程解疑
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>


圖片說明

最佳回答:


1。安卓 有這個控件 Chronometer,你最好用這個;自己做,肯定要用到線程;
2.我估計你是線程崩潰,引起的崩潰;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved