2017-05-28 2 views
0

Ich möchte einen Countdown erstellen, wo die ursprüngliche Nummer vom Benutzer angegeben wird. Ich habe diese Anwendung erstellt, aber wenn die Anwendung heruntergefahren wird. Ich verstehe nicht, was der Fehler ist, weil es während der Kompilierung keine Fehler gibt. Was mache ich falsch? Dies ist der Code:Countdown mit editText

public class Main2Activity extends AppCompatActivity { 

Button bstart, bStopReset; 
EditText editTimer; 
CountDownTimer timer; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

    editTimer = (EditText) findViewById(R.id.contatore); 
    bstart = (Button) findViewById(R.id.start); 
    bStopReset = (Button) findViewById(R.id.stop_reset); 
} 

String valore = editTimer.getText().toString(); 

int ValoreIntero = Integer.parseInt(valore); 


public void startOnClick(View view) { 

    timer = new CountDownTimer(ValoreIntero, 1000) { 
     @Override 
     public void onTick(final long millSecondsLeftToFinish) { 
      String time = String.valueOf(millSecondsLeftToFinish/1000); 
      editTimer.setText(time); 
     } 

     @Override 
     public void onFinish() { 
      editTimer.setText("Done!"); 
     } 
    }; 
    timer.start(); 
} 

public void stopOnClick(View view) { 
    timer.cancel(); 
    editTimer.setText("0"); 
    } 

} 

xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main2" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/holo_blue_bright" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:weightSum="1" 
tools:context="com.example.gabrypacor.orologio.Main2Activity"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.39" 
    android:gravity="center" 
    android:text="countdown" 
    android:textAlignment="center" 
    android:textSize="36sp" /> 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.27" 
    android:gravity="center" 
    android:text="inserisci il tempo del countdown nel riquadro blu" 
    android:textAlignment="center" 
    android:textSize="20sp" 
    tools:textAlignment="center" /> 

<EditText 
    android:id="@+id/contatore" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.21" 
    android:background="@android:color/holo_blue_light" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:text="0" 
    android:textAlignment="center" 
    android:textColorLink="?android:attr/textColorPrimaryDisableOnly" 
    android:textSize="60sp" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.18" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/start" 
     android:layout_width="wrap_content" 
     android:layout_height="70dp" 
     android:layout_weight="1" 
     android:onClick="startOnClick" 
     android:text="inizio" /> 

    <Button 
     android:id="@+id/stop_reset" 
     android:layout_width="wrap_content" 
     android:layout_height="70dp" 
     android:layout_weight="1" 
     android:onClick="stopOnClick" 
     android:text="stop/reset" /> 
</LinearLayout> 

+0

Sie sollten Logcat Log suchen, um herauszufinden, die Ursache des Fehlers –

+0

Thera sind kein Fehler –

+0

Timer funktioniert ordnungsgemäß? Oder Sie verwenden Log.d ("ETWAS", "Inhalt"); oder System.out.println ("okay"); und schau dir logcat an. Setzen Sie es irgendwo, wenn das Programm startet oder in der Timer-Methode. –

Antwort

1

Sie setzen diese beiden Werte, wenn Sie Ihre Aktivität initialisiert wird, statt, wenn der Benutzer klickt auf den Startknopf:

String valore = editTimer.getText().toString(); 
int ValoreIntero = Integer.parseInt(valore); 

Verschieben Sie sie in Ihre startOnClick-Methode und das sho achte die Ausnahme, die du bekommst.