【问题标题】:Timer App closing out after second onClick of the START buttonTimer App 在点击 START 按钮的第二次 onClick 后关闭
【发布时间】:2016-04-02 08:04:53
【问题描述】:

我对 android 很陌生,正在制作一个计时器应用程序(代码不完整,如您所见,但我正在一次测试)。第二次点击开始按钮会导致应用程序关闭。我编辑了我的代码,以便在每次调用 onClick 时重新创建计时器,但在第二次单击后应用程序仍然关闭。谢谢你的帮助。

package com.example.ryan.timerapp;

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.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    secondsView = (TextView) findViewById(R.id.seconds);
    tenSecondsView = (TextView) findViewById(R.id.tensSeconds);
    hoursView = (TextView) findViewById(R.id.hours);
    tenHoursView = (TextView) findViewById(R.id.tensHours);
    minutesView = (TextView) findViewById(R.id.minutes);
    tenMinutesView = (TextView) findViewById(R.id.tensMinutes);

       timer = new Timer();

}

 TextView secondsView;
 TextView tenSecondsView;
 TextView hoursView;
 TextView tenHoursView;
 TextView minutesView;
 TextView tenMinutesView;

    Timer timer;
    TimerTask timerTask;


    public void start(View view){

        if(timer != null) {
            timerTask.cancel();
            timer.cancel();
        }
        timer = new Timer();
         timerTask = new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Thread(new Runnable() {
                    @Override
                    public void run() {

                        int secondsInt = Integer.parseInt(secondsView.getText().toString());

                        int tenSecondsInt = Integer.parseInt(tenSecondsView.getText().toString());

                        int hoursInt = Integer.parseInt(hoursView.getText().toString());

                        int tenHoursInt = Integer.parseInt(tenHoursView.getText().toString());

                        int minutesInt = Integer.parseInt(minutesView.getText().toString());

                        int tenMinutesInt = Integer.parseInt(tenMinutesView.getText().toString());
                        if (secondsInt < 9) {
                            secondsInt++;
                            secondsView.setText(secondsInt + "");
                        } else {
                            secondsInt = 0;
                            secondsView.setText(secondsInt + "");
                            if (tenSecondsInt < 6) {
                                tenSecondsInt++;
                                tenSecondsView.setText(tenSecondsInt + "");
                            } else {
                                tenSecondsInt = 0;
                                tenSecondsView.setText(tenSecondsInt + "");
                                if (minutesInt < 9) {
                                    minutesInt++;
                                    minutesView.setText(minutesInt + "");
                                } else {
                                    minutesInt = 0;
                                    minutesView.setText(minutesInt + "");
                                }
                            }
                        }
                    }
                }));

            }
        };
        timer.scheduleAtFixedRate(timerTask,0, 1000);

    }



public void stop(View view){
    timer.cancel();
}

public void reset(View view){

    secondsView.setText("0");
    tenSecondsView.setText("0");

    minutesView.setText("0");

    tenMinutesView.setText("0");

    hoursView.setText("0");

    tenHoursView.setText("0");

}


}

XML:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
    tools:context="com.example.ryan.timerapp.MainActivity"
    android:layout_margin="5dp"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:text="Timer"
        android:gravity="center"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="2"
        >
        <TextView
            android:id="@+id/tensHours"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
        <TextView
            android:id="@+id/hours"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
        <TextView
            android:id="@+id/firstColon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text=":"
            />
        <TextView
            android:id="@+id/tensMinutes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
        <TextView
            android:id="@+id/minutes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
        <TextView
            android:id="@+id/secondColon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text=":"
            />
        <TextView
            android:id="@+id/tensSeconds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
        <TextView
            android:id="@+id/seconds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="0"
            />
    </LinearLayout>

    <Button
        android:id="@+id/startButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="START"
        android:textSize="30sp"
        android:onClick="start"
        />
    <Button
        android:id="@+id/stopButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="STOP"
        android:textSize="30sp"
        android:onClick="stop"
        />
    <Button
        android:id="@+id/resetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_weight="2"
        android:text="RESET"
        android:textSize="30sp"
        android:onClick="reset"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    </LinearLayout>

【问题讨论】:

  • 您可以发布 XML 文件,以便我可以尝试重新创建它吗?
  • 我发布了它,但 xml 的顶部说:schemas.android.com/apk/res/android" xmlns: tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.ryan.timerapp.MainActivity" android:layout_margin="5dp" >
  • 它不会显示,但它就在那里
  • 将 XML 剪切并粘贴到单独的代码段中,而不是与 java 捆绑在一起

标签: android android-fragments timer timertask


【解决方案1】:

第一个是秒计数器没有更新超过 1 秒

TimerTask 用于在指定时间运行特定任务。 timer.schedule() 并不意味着连续运行。 检查此documentation。 所以这段代码-

 int secondsInt = Integer.parseInt(txt.getText().toString()); 
                    secondsInt++;

                    if(secondsInt<=6) {
                        txt.setText(secondsInt + "");
                    }

只会运行一次。因此

秒计数器在 1 秒后未更新

是明显的行为。

再次点击开始按钮会导致应用关闭

您正在尝试运行已安排好的TimerTask。 您需要创建一个新的 Timer 实例才能再次启动您的 TimerTask

改变你的 start() 方法 -

public void start(View view){

        if(timer != null) {
                    timerTask.cancel();
                    timer.cancel();
                }
                timer = new Timer();
                timerTask = new TimerTask() {
                    @Override
                    public void run() {

                        runOnUiThread(new Thread(new Runnable() {
                            @Override
                            public void run() {
                                int secondsInt = Integer.parseInt(txt.getText().toString());
                                secondsInt++;

                                if(secondsInt<=6) {
                                    seconds.setText(secondsInt + "");
                                }
                            }
                        }));

                    }
                };
                timer.scheduleAtFixedRate(timerTask,0, 1000);

    }

此外,无需一次又一次地初始化您的 TextView 。只在onCreate() 做一次 -

private TextView seconds; 
  private Timer timer;
  private TimerTask timerTask;

在 onCreate() 中

seconds = (TextView) findViewById(R.id.seconds);

【讨论】:

  • 我创建了新的实例,一切都适用于第一次启动/暂停/重置。之后,当点击开始按钮时,它会关闭
  • 我更新了我的代码,但是在第 47 行的 timeTask.cancel(); 中抛出了一个错误;行
  • 错误是:java.lang.IllegalStateException: Could not execute method for android:onClick(错误位于第 47 行)
  • 您正在编写 TimerTask timerTask = { in start() 但我的代码有 timerTask = new TimerTask() 。请复制我的代码。在私有 TimerTask timerTask 之外声明 timerTask;它肯定会奏效。
  • 删除计时器 = new Timer();来自 onCreate()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多