Android Studio:启动倒数计时器时应用程序崩溃

时间:2016-05-23 15:42:26

标签: android crash countdowntimer

我制作了一个应用程序来计算比赛中的得分目标(当你和朋友一起玩,而你又懒得计算得分时:D)我想制作一个倒数计时器来计算比赛的时间。 当我按下“开始”按钮以启动倒数计时器时,我的应用程序崩溃。我有2个活动。主要活动是计算2个团队得分的代码。

这是我的第二个活动代码(CountDowntimer应该在哪里。):

getData('urlTo@RequestMappingincontroller', function(xhr) {
    if (xhr.readyState === 4 && xhr.response < 400) {
        var data = JSON.parse(xhr.responseText);
​
        data.forEach(function(value,index,array){
            console.log(value);
        });
    }
});

这是我的布局代码:

    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.");


        }
    }





}

1 个答案:

答案 0 :(得分:0)

您的代码:

TextView TimerText = (TextView) findViewById(R.id.Timer);

更改此行,只需删除&#34; TextView&#34;,它已经位于首位。

TimerText = (TextView) findViewById(R.id.Timer);