gpt4 book ai didi

java - 添加一些代码行后,应用崩溃

转载 作者:行者123 更新时间:2023-12-02 02:40:56 24 4
gpt4 key购买 nike

添加一些代码行后,单击按钮后我的应用程序崩溃。而且我不知道为什么。
该应用程序的目的只是尽可能快地双击,它显示了从第一次点击到第二次点击所需的时间(以毫秒为单位)。
我尝试在右上角添加高分。但是在我添加了int highScoreInt = (int) (startTime - millisUntilFinished);int currentScoreInt = (int) (startTime - millisUntilFinished);以将这些计时器值存储为该if语句的整数之后

if (currentScoreInt >= highScoreInt) {
highScore.setText(highScoreInt);
}

我的应用程序崩溃了。

这是XML`
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/currentScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startStopButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="99dp"
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="250dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000000"
android:text="Start / Stop"
android:textSize="25dp"
android:textColor="#39FF14" />

<TextView
android:id="@+id/bestText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="18dp"
android:layout_marginTop="23dp"
android:layout_toStartOf="@+id/highScore"
android:text="Best:"
android:textColor="#39FF14"
android:textSize="20dp"
android:textStyle="bold" />

<TextView
android:id="@+id/highScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/bestText"
android:layout_alignParentEnd="true"
android:layout_marginEnd="14dp"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="#39FF14"
android:text="---" />

`

这是Java
public class MainActivity extends AppCompatActivity {

private final long startTime = 30000;
private final long interval = 100;
private boolean buttonClicked;
private Button startStopButton;
private TextView currentScore;
private int highScoreInt;
private TextView highScore;
private TextView bestText;

CountDownTimer timer = new CountDownTimer(startTime, interval) {

@Override
public void onTick(long millisUntilFinished) {
currentScore.setText("" + (startTime - millisUntilFinished));
highScore.setText("" + (startTime - millisUntilFinished));

int highScoreInt = (int) (startTime - millisUntilFinished);
int currentScoreInt = (int) (startTime - millisUntilFinished);

if (currentScoreInt >= highScoreInt) {
highScore.setText(highScoreInt);
}
}

@Override
public void onFinish() {
}
};

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

currentScore = (TextView) findViewById(R.id.currentScore);
highScore = (TextView) findViewById(R.id.highScore);
bestText = (TextView) findViewById(R.id.bestText);
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;
}
}
});
}

}

here's the logcat screenshot

谢谢!

最佳答案


if (currentScoreInt >= highScoreInt) {
highScore.setText(highScoreInt);
}

请使用 highScore.setText(String.valueOf(highScoreInt));

关于java - 添加一些代码行后,应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com