在我添加了一些代码行后,我的应用程序在单击按钮后崩溃了。我不明白为什么。
该应用程序的目的是尽可能快地双击,它显示从第一次点击到第二次点击所需的时间(以毫秒为单位)。
我尝试在右上角添加一个高分。但是在我添加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;
}
}
});
}
}
谢谢!
答案 0 :(得分:0)
在
if (currentScoreInt >= highScoreInt) {
highScore.setText(highScoreInt);
}
请使用highScore.setText(String.valueOf(highScoreInt));