单击按钮后我的应用程序崩溃

My app crashes after clicking button

当我点击按钮时,我的应用程序崩溃了,我不知道为什么。我已经多次查找我的代码,但似乎找不到任何 "major flaws"。该应用程序的目的只是尽可能快地点击按钮两次,它会显示从第一次点击到第二次点击所需的时间。某种反应。我使用了计时器,因为我不知道还能用什么。 (开始时间的值)-(当前计时器的值)=(第一次和第二次点击之间的时间)。

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context="com.keklabs.reactiontimer.MainActivity">


    <TextView
        android:id="@+id/scoreText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:text="Click below to start!"
        android:textColor="#39FF14"
        android:textSize="30dp" />

    <Button
        android:id="@+id/startStopButton"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#000000"
        android:text="Start / Stop"
        android:textSize="25dp"
        android:textColor="#39FF14" />

</RelativeLayout>


public class MainActivity extends AppCompatActivity {

    private final long startTime = 0;
    private final long interval = 100;
    private boolean buttonClicked;
    private Button startStopButton;
    private TextView scoreText;

    CountDownTimer timer = new CountDownTimer(30000, 100) {

        @Override
        public void onTick(long millisUntilFinished) {
            scoreText.setText("kek"); //displayed text is value of starttime (30000) - current value of timer,
        }                             //some sort of reaction game thing

        @Override
        public void onFinish() {
        }
    };

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

        TextView scoreText = (TextView) findViewById(R.id.scoreText);
        Button startStopButton = (Button) findViewById(R.id.startStopButton);


        startStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!buttonClicked) {
                    timer.start();
                    buttonClicked = true;
                }
                else {
                    timer.cancel();
                    buttonClicked = false;
                }
            }
        });

    }

}

编辑

如何在 scoreText.setText(startTime - millisUntilFinished); 行中以正确的方式显示 startTime-当前计时器值?

CountDownTimer 计时器 = new CountDownTimer(startTime, interval) {

    @Override
    public void onTick(long millisUntilFinished) {
        scoreText.setText(startTime - millisUntilFinished);
    }

    @Override
    public void onFinish() {
    }
};

实际上,您要声明 TextView scoreText 两次。一个是全局的,另一个是本地的。您可以将 TextView scoreText = (TextView) findViewById(R.id.scoreText); 替换为 scoreText = (TextView) findViewById(R.id.scoreText); 在你的 onCreate()