我制作了一个应用程序来计算比赛中的进球数(当您和 friend 一起玩并且懒得计算分数时:D),我想创建一个倒数计时器来计算比赛时间。
当我按下开始按钮启动倒数计时器时,我的应用程序崩溃了。我有2个 Activity 。主要 Activity 是用于计算2个团队得分的代码。
这是我的第二个 Activity 代码(CountDowntimer应该在哪里):
package com.example.robert.scorecount;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class SecondActivity extends AppCompatActivity {
Button StartButton,StopButton;
TextView TimerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button StartButton = (Button) findViewById(R.id.StartButton);
Button StopButton = (Button) findViewById(R.id.StopButton);
TextView TimerText = (TextView) findViewById(R.id.Timer);
TimerText.setText("00:00:00");
final CounterClass timer = new CounterClass(3600000,1000);
assert StartButton != null;
StartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
assert StopButton != null;
StopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
public class CounterClass extends CountDownTimer{
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
String HourMinutesSeconds = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
TimerText.setText(HourMinutesSeconds);
}
@Override
public void onFinish() {
TimerText.setText("Finished.");
}
}
}
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.robert.scorecount.SecondActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Timer Settings"
android:id="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:textAlignment="center" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/StartButton"
android:layout_marginBottom="37dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:id="@+id/Timer"
android:textSize="30dp"
android:layout_marginTop="88dp"
android:layout_below="@+id/ThirtyMinMatch"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/StopButton"
android:layout_above="@+id/StartButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30 Minutes"
android:id="@+id/ThirtyMinMatch"
android:layout_marginTop="24dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/Timer"
android:layout_toStartOf="@+id/Timer" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 Hour"
android:id="@+id/OneHourMinMatch"
android:layout_alignTop="@+id/ThirtyMinMatch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/Timer"
android:layout_toEndOf="@+id/Timer" />
</RelativeLayout>
最佳答案
您的代码:
TextView TimerText = (TextView) findViewById(R.id.Timer);
更改此行,只需删除“TextView”,它已经在顶部。
TimerText = (TextView) findViewById(R.id.Timer);