简体   繁体   English

每当我实现我的 CountDownTimer 代码时,我的移动应用程序就会崩溃

[英]My mobile app crashes whenever I implement my CountDownTimer code

package com.example.prototypeb.ui.game.Game_components;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.prototypeb.R;
import com.example.prototypeb.ui.game.GameFragment;


public class Game_adverbs extends AppCompatActivity {

    TextView timer;
    CountDownTimer countdown;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_adverbs);
        setTitle("Adverbs");
        countdown.start();

        Button backbutton = findViewById(R.id.backbtn1);
        backbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), GameFragment.class);
                startActivity(intent);
            }
        });

        timer = findViewById(R.id.text_view_timer);
        countdown = new CountDownTimer(10000, 1000) {
            public void onTick(long millisUntilFinished) {
                timer.setText((int) (millisUntilFinished/1000));
            }

            public void onFinish() {
                timer.setText("TIME'S UP!");
            }
        };
    }
}

here is part of my code for an activity.这是我的活动代码的一部分。 I actually want my timer to start right away when my activity is started.我实际上希望我的计时器在我的活动开始时立即启动。 My app crashes instantly as soon as my activity is started and I have tested and identified that the problem is within/ around the CountDownTimer code一旦我的活动开始,我的应用程序就会立即崩溃,并且我已经测试并确定问题出在 CountDownTimer 代码内部/周围

The exception is arising because you're calling countdown.start() before initializing the countdown object.出现异常是因为您在初始化countdown object 之前调用了countdown.start()

So moving countdown.start();所以移动countdown.start(); after initializing countdown , or at the bottom of onCreate method may work.在初始化countdown之后,或者在onCreate方法的底部可能会起作用。

Something like this...像这样的东西...

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_adverbs);
    setTitle("Adverbs");
    
    Button backbutton = findViewById(R.id.backbtn1);
    backbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), GameFragment.class);
            startActivity(intent);
        }
    });

    timer = findViewById(R.id.text_view_timer);
    countdown = new CountDownTimer(10000, 1000) {
        public void onTick(long millisUntilFinished) {
            timer.setText((int) (millisUntilFinished/1000));
        }

        public void onFinish() {
            timer.setText("TIME'S UP!");
        }
    };
    countdown.start();
}

You are calling countdown.start();您正在调用countdown.start(); before defining the countdown object在定义countdown object 之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我的EditText为空并且按下按钮时,我的应用程序就会崩溃 - My app crashes whenever my EditText is empty and I press a button 每当我在色轮外滑动时,我的应用程序就会崩溃 - my app crashes whenever i swipe outside the color wheel 每当我尝试更改活动时,我的应用就会崩溃 - My app crashes whenever i try to change activities 我的应用程序在启动时崩溃 - Android Studio - My App crashes whenever it is launched - Android Studio 计时器代码使我的应用崩溃 - Timer code crashes my app 每当我添加 OnClickListener 或 Fragment 时,我的应用程序就会崩溃,这是我的 OnClickListener 代码 - My app crashed whenever i add a OnClickListener or a Fragment , here's my code with the OnClickListener 每当我在 andriod studio EditText 框中执行诸如添加子等于等任何操作时,我的应用程序都会崩溃 - whenever i perform any operations like add sub equals etc in andriod studio EditText box , my app crashes 每当我单击“后退/主页”按钮时,代码的asynctask部分就会关闭/停止应用 - The asynctask part of my code closes/stops the app whenever I click the back/home button 我的 Volley 应用程序崩溃,可能在 listView 代码上 - My Volley app crashes, perhaps on the listView code 每当我 go 进入活动时,该应用程序就会崩溃 - The app just crashes whenever I go into the activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM