2016-03-23 10 views
0

編集テキストボックスに分を入力してから、送信ボタンをクリックすると、カウンタが開始されます。しかし、ここのエラーは何ですか?残念ながら、アプリが停止していることを示しています。助けてください。ボタンクリック時のanroid countdownTimerエラー

 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 

答えて

0

あなたはonCreate()に整数にEditText値を変換しようとしている - その時点であなたの EditTextが空になり、 et1.getText().toString()"" ひいてはなり、onCreate()で今

int min =Integer.parseInt(et1.getText().toString()); 

Integer.parseInt("")は明らかにスローされます。NumberFormatException

また、それはdoesnのように、あなたのxmlであなたの EditTextは数字だけを受け入れるように作る空の文字列のための

  1. まずチェック

    if (!et1.getText().toString().isEmpty()){ 
        //convert here 
    } 
    
  2. - あなたは2つの、最も重要なことを行う必要があり、これを回避するために0

    今後もNumberFormatExceptionを投げます。

+0

xmlファイルのeditText番号のみを作る方法はありますか? –

+0

これを加える - android:inputType = "number" –

+0

ありがとう! \t 素晴らしい!それは今働いている!もう1つのこと:coutdownが実行されているときに別の番号を入力するとします。両方のカウントダウンが重複しています。前のものが消えて最後のものが始まるように、この問題をどうやって止めることができますか? –

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) { 
        //(...) 

あなたまた、GetFormatの例外を避けるために、EditTextが空でない場合は前に検証します。

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

あなたが言ったように私はコードを書いたが、提出をクリックすると "完了"と表示された。カウントダウンが開始されていません –

+0

countDownTimerは秒単位ではなく、ミリ秒でなければなりません。 (ちょうど私のコードを編集しました) – PedroHawk

+0

素晴らしい!それは今働いている!もう1つのこと:coutdownが実行されているときに別の番号を入力するとします。両方のカウントダウンが重なっている。前のものが消えて最後のものが始まるように、この問題をどうやって止めることができますか? –

関連する問題