简体   繁体   English

单击按钮时,Android Countdown Timer应用程序崩溃

[英]Android Countdown Timer app crashes when the button is clicked

I've just started coding in Android SDK and this is my first attempt at a Countdown Timer. 我刚刚开始使用Android SDK进行编码,这是我首次尝试使用倒数计时器。 I've set the timer to start at the press of the button with setOnClickListener(). 我使用setOnClickListener()将计时器设置为在按下按钮时开始。 The timer doesn't start but the app stops working when I click the button. 计时器没有启动,但是当我单击按钮时,应用程序停止运行。 The device displays a message saying the app has stopped working. 设备显示一条消息,说明该应用程序已停止运行。

I've written a method to format the time into minutes and seconds. 我已经编写了一种将时间格式化为分钟和秒的方法。 And i've set the call in the onTick() method of the CountDownTimer. 我已经在CountDownTimer的onTick()方法中设置了调用。

I've posted my code below. 我已经在下面发布了我的代码。 Please take a look and point me to where the problem is and what to do. 请看一看,并指出问题所在和解决方法。 Much appreciated. 非常感激。

package com.example.day1;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button; 
import android.widget.TextView;
public class MainActivity extends Activity {


TextView Timer;//Textview to display the timer
    //This is the method to format the time into minutes and seconds 
public String formatTime(long millis)
{
    String output="00:00";
    long second=millis/1000;
    long minute=second/60;
    second=second%60;
    minute=minute%60;
    String sec=String.valueOf(second);
    String min=String.valueOf(minute);
    output=min+":"+sec;
    return output;
}

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countdown);
    Timer=(TextView) findViewById(R.id.text1);
    Button button1=(Button)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


    new CountDownTimer(60000,1000)
    {
        public void onTick(long millisUntilFinished){
    Timer.setText(formatTime(millisUntilFinished));
        }
    public void onFinish()
    {
        onStop();
    }
    }.start();
    }
         });
     }
         }

(1) Your app crash because you do not implements your activity with OnClickListener: you need to extend the method of your activity onclicklistener evet (1)您的应用程序崩溃,因为您未使用OnClickListener实现活动:您需要扩展活动方法onclicklistener evet

Here is the code 这是代码

package com.example.day1;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button; 
import android.widget.TextView;

///////////////////The solution is fix here///////////////
public class MainActivity extends Activity implements View.OnClickListener {
///////////////////End of solution//////////

TextView Timer;//Textview to display the timer
    //This is the method to format the time into minutes and seconds 
public String formatTime(long millis)
{
    String output="00:00";
    long second=millis/1000;
    long minute=second/60;
    second=second%60;
    minute=minute%60;
    String sec=String.valueOf(second);
    String min=String.valueOf(minute);
    output=min+":"+sec;
    return output;
}

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countdown);
    Timer=(TextView) findViewById(R.id.text1);
    Button button1=(Button)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


    new CountDownTimer(60000,1000)
    {
        public void onTick(long millisUntilFinished){
    Timer.setText(formatTime(millisUntilFinished));
        }
    public void onFinish()
    {
        onStop();
    }
    }.start();
    }
         });
     }
         }

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

相关问题 Android 点击Button时App崩溃,startActivity - Android App Crashes when Button is clicked, startActivity 单击按钮时应用崩溃 - App Crashes when Button is Clicked Android-单击按钮以显示具有ListView的活动时,应用程序崩溃 - Android - App crashes when button is clicked to show an activity with listview 单击按钮后android应用崩溃的原因 - Reason behind android app crashes when button is clicked 当TextInputLayout为空并单击按钮时,应用程序崩溃 - App crashes when TextInputLayout empty and button is clicked 单击Facebook登录按钮会使应用程序崩溃 - Facebook Login Button crashes app when clicked Android 单击搜索图标时应用程序崩溃? - Android App crashes when clicked on the search Icon? 单击Android登录按钮会使应用程序崩溃。 按钮验证使用Sqlite数据库的登录 - Android login button crashes app when clicked. Button validates login with Sqlite database 当我单击该按钮时,我的应用程序崩溃,这将带我进入Java,Android Studio上的另一个活动 - My app crashes when I clicked the button which will take me the another Activity on Java, Android Studio Firebase Auth Login and Signup 应用程序在单击按钮时崩溃 - Firebase Auth Login and Signup app crashes when button clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM