0
我想創建一個倒計時,其中的初始數字由用戶給出。我創建了這個應用程序,但是當應用程序開始關閉時。我不明白這個錯誤是什麼,因爲在編譯過程中沒有錯誤。我究竟做錯了什麼? 這是代碼:與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>
您應該看看logcat的日誌找出錯誤的根本原因 –
thera沒有錯誤 –
計時器工作正常嗎?或者你可以使用Log.d(「SOMETHING」,「Content」);或System.out.println(「okay」);並看看logcat。當程序啓動或定時器方法時,將它放在某處。 –