简体   繁体   English

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

[英]My app crashes after clicking button

When I click the button my app crashes and I have no idea why. 当我单击按钮时,我的应用程序崩溃了,我也不知道为什么。 I've looked up my code a lot of times but can't seem to find any "major flaws". 我已经查询了很多次代码,但似乎找不到任何“重大缺陷”。 The purpose of the app is just clicking the button twice as fast as possible and it displays the amount of time needed from the first tap to the second tap. 该应用程序的目的是尽可能快地单击按钮两次,它显示从第一次点击到第二次点击所需的时间。 Some sort of reaction thing. 某种反应的东西。 I used a timer because I didn't know what else to use. 我使用了计时器,因为我不知道还要使用什么。 (Value of start time) - (value of current timer) = (time between first and second click). (开始时间的值)-(当前计时器的值)=(第一次单击和第二次单击之间的时间)。

    <?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;
                }
            }
        });

    }

}

EDIT 编辑

How do i do it the right way in the line scoreText.setText(startTime - millisUntilFinished); 我该怎么做正确的方法在一行scoreText.setText(startTime - millisUntilFinished); to display startTime-current timer value? 显示startTime-当前计时器值?

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

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

    @Override
    public void onFinish() {
    }
};

Actually, you are declaring your TextView scoreText twice. 实际上,您两次声明了TextView scoreText One gloabally and other locally. 一个人在全球范围内,其他人则在本地。 You can replace TextView scoreText = (TextView) findViewById(R.id.scoreText); 您可以替换TextView scoreText = (TextView) findViewById(R.id.scoreText); to scoreText = (TextView) findViewById(R.id.scoreText); scoreText = (TextView) findViewById(R.id.scoreText); in your onCreate() 在你的onCreate()中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM