开发者社区> 问答> 正文

倒计时程序的崩溃问题 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>

图:screenshot

展开
收起
爵霸 2016-03-03 11:53:48 2799 0
1 条回答
写回答
取消 提交回答
  • 方法一: 利用java的类Timer,TimerTask还有android的Handler 界面welcome_activity.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <TextView android:id="@+id/count_down" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="16dp" android:layout_marginTop="16dp" android:gravity="center" android:textSize="32sp" android:textColor="#50000000" android:background="@drawable/count_down_background" tools:text="2" /> </RelativeLayout> package com.example.counttimer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.widget.TextView; public class WelcomeActivity extends Activity { private final static int COUNT = 1; private TextView countDown; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_welcome); initView(); } //sehedule的第而个参数是第一次启动延时的时间,第三个是每隔多长时间执行一次。单位都是ms。  //因此这里是每一秒发送一次消息给handler更新UI。  //然后三秒后时间到了,在timer的第二个sehedule中进行跳转到另外一个界面 private void initView() { countDown = (TextView) findViewById(R.id.count_down); final Timer timer = new Timer(); final long end = System.currentTimeMillis() + 1000*3; timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(COUNT); } }, 0, 1000); //这里的schedule的第二个参数意义是到了这个时间尽快运行run里面的方法 timer.schedule(new TimerTask() { @Override public void run() { Intent i = new Intent(WelcomeActivity.this, SecondActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); finish(); timer.cancel(); } }, new Date(end)); } private Handler handler = new Handler(){ int num = 2; public void handleMessage(android.os.Message msg) { switch (msg.what) { case COUNT: countDown.setText(String.valueOf(num)); num--; break; default: break; } }; }; } count_down_background.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=" http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8dp"/> <solid android:color="#1e000000"/> </shape> 方法二 : 利用android封装的类CountDownTimer。其实内部也是用Handler实现的。其他都一样。 package com.example.counttimer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.view.Window; import android.widget.TextView; public class WelcomeActivity extends Activity { private final static int COUNT = 1; private TextView countDown; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_welcome); initView(); } private void initView() { countDown = (TextView) findViewById(R.id.count_down);    //CountDownTimer构造器的两个参数分别是第一个参数表示总时间,第二个参数表示间隔时间。    //意思就是每隔xxx会回调一次方法onTick,然后xxx之后会回调onFinish方法。 CountDownTimer timer = new CountDownTimer(3200,1000) { int num = 2; @Override public void onTick(long millisUntilFinished) { countDown.setText(String.valueOf(num)); num--; } @Override public void onFinish() {          //计时完成调用 Intent i = new Intent(WelcomeActivity.this, SecondActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); finish(); } }; timer.start(); } }

    答案来源于网络

    2019-10-16 18:55:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载