单击按钮时出现Android countdownTimer错误

时间:2016-03-23 23:37:01

标签: java android

我希望用户在编辑文本框中输入分钟,然后在单击提交按钮时,计数器将启动。但这里的错误是什么?它表明,不幸的是应用程序已停止。请帮忙。

      package com.example.asifsabir.counterapps;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    TextView tv1;
    EditText et1;
    Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.textView1);
        et1 =(EditText) findViewById(R.id.et1);
        bt1= (Button) findViewById(R.id.bt1);



        bt1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int min =Integer.parseInt(et1.getText().toString());
                        if (!et1.getText().toString().isEmpty()){
                            final int sec = min*60;

                            new CountDownTimer(sec, 1000) {

                                public void onTick(long millisUntilFinished) {
                                    tv1.setText("" + String.format("%02d:%02d",
                                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
                                                    TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                                                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                                }

                                public void onFinish() {
                                    tv1.setText("done!");
                                }
                            }.start();
                        }

                    }
                }
        );


    }

}

,xml代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et1"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:inputType="number"
        android:hint="enter sec"
        android:textAlignment="center"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:layout_gravity="center"
        android:text="submit"/>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="count"
        android:layout_marginTop="100dp"
        android:layout_gravity="center"
        android:textColor="#3559da"
        android:textSize="80sp" />



    </LinearLayout>

错误日志:

339-25339/? I/art: Late-enabling -Xcheck:jni
03-24 05:25:44.927 25339-25339/? I/art: VMHOOK: rlim_cur : 0 pid:25339
03-24 05:25:44.957 25339-25353/? I/art: Debugger is no longer active
03-24 05:25:45.127 25339-25339/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.asifsabir.counterapps, PID: 25339
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asifsabir.counterapps/com.example.asifsabir.counterapps.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:155)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5702)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
                                                    Caused by: java.lang.NumberFormatException: Invalid int: ""
                                                       at java.lang.Integer.invalidInt(Integer.java:138)
                                                       at java.lang.Integer.parseInt(Integer.java:358)
                                                       at java.lang.Integer.parseInt(Integer.java:334)
                                                       at com.example.asifsabir.counterapps.MainActivity.onCreate(MainActivity.java:29)
                                                       at android.app.Activity.performCreate(Activity.java:5958)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:155) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5702) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 
03-24 05:28:37.087 25339-25339/com.example.asifsabir.counterapps D/Process: killProcess, pid=25339
03-24 05:28:37.087 25339-25339/com.example.asifsabir.counterapps D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

2 个答案:

答案 0 :(得分:0)

当点击事件发生时你应该得到Integer.parseInt,你的输入必须是毫秒:

bt1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int min = Integer.parseInt(et1.getText().toString());
                    int sec = min*60;
                    int millis = sec*1000;
                    new CountDownTimer(millis, 1000) {
                    //(...)

如果EditText不为空,你还应该验证,以避免像你得到的NumberFormat异常。

if (et1.getText().toString().isEmpty())
   //notice user to input some number
   return;

答案 1 :(得分:0)

您正尝试将EditText值转换为onCreate() -

中的整数
int min =Integer.parseInt(et1.getText().toString());

现在在onCreate(),那时你的 EditText将为空 et1.getText().toString()将为"" 因此 Integer.parseInt("")显然会抛出NumberFormatException

要避免这种情况,您需要做两件最重要的事情 -

  1. 首先检查空字符串

    if (!et1.getText().toString().isEmpty()){
       //convert here
    }
    
  2. 同时让xml中的EditText仅接受数字,以便将来不再发送NumberFormatException