【发布时间】:2018-06-18 23:01:00
【问题描述】:
添加几行代码后,我的应用在单击按钮后崩溃。我不知道为什么。
该应用程序的目的只是尽可能快地双击,它会向您显示从第一次点击到第二次点击所需的时间量(以毫秒为单位)。
我尝试在右上角添加一个高分。但是在我添加 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;
}
}
});
}
}
谢谢!
【问题讨论】:
-
还有 logcat 中的异常?
-
highScore.setText(String.valueOf(highScoreInt)); -
LogCat 中还是一样的异常吗?因为 LogCat 表明 Daniel 关于导致初始崩溃的原因是正确的
-
如果您不使用@JoeC,他将不会收到返回查看的通知。您犯的两个错误需要纠正以消除反对票:忽略在您的问题中包含 logcat 消息的文本并且没有正确地表达您的问题。您刚刚说“应用程序崩溃”。而是说,“该应用程序因以下错误而崩溃,我不明白错误日志可以有人解释它告诉我的内容”或类似的内容,然后复制并粘贴 LogCat 而不是包含屏幕截图。这听起来可能微不足道,但事实并非如此。
-
第三个错误(正如@JoeyHarwood 已经提到的)是提供 logcat 的屏幕截图而不是文本本身。这就是为什么我之前检查时没有看到它的原因。见:Why not to upload images of code on SO when asking a question?