Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的计时器重置按钮在计时器应用程序中工作异常?_Java_Android - Fatal编程技术网

Java 为什么我的计时器重置按钮在计时器应用程序中工作异常?

Java 为什么我的计时器重置按钮在计时器应用程序中工作异常?,java,android,Java,Android,我对安卓系统比较陌生。我认为创建一个简单的应用程序,比如计时器,将是一个很好的开始方式 我设法创建了我的简单计时器应用程序,但遇到了一个问题 我让我的应用程序启动并暂停,但我不知道如何将其重置回“00:00:00”。我在Java代码的第56-59行中尝试了以下内容 package com.awesomeapps.misael.timer; import android.app.Activity; import android.os.Bundle; import android.os.Handl

我对安卓系统比较陌生。我认为创建一个简单的应用程序,比如计时器,将是一个很好的开始方式

我设法创建了我的简单计时器应用程序,但遇到了一个问题

我让我的应用程序启动并暂停,但我不知道如何将其重置回“00:00:00”。我在Java代码的第56-59行中尝试了以下内容

package com.awesomeapps.misael.timer;

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

public class MainActivity extends Activity {

    private Button startButton;
    private Button pauseButton;

    private TextView timerValue;

    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

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

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

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.pauseButton);

        pauseButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);

            }
        });
    }
    public void onButtonTapReset(View v)
    {
        timerValue.setText("00:00:00");
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };

}
当我按下重置按钮时,它确实会变为“00:00:00”,但当按下启动按钮时,计时器会在停止的位置恢复。例如,如果计时器在“00:05:123”时,我会按reset,它会变为“00:00:00”,但当我再次按start时,它会从停止的位置开始,或者可能更像“00:08:456”,当我按reset然后再次启动时,它不会从零开始

有人能帮我弄清楚我的重置按钮吗

我已经附加了一些文件,我认为我的错误可能是在上面包含Java代码

字符串xml文件

<resources>
    <string name="app_name">Timer</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="timerVal">00:00:00</string>
    <string name="pauseButtonLabel">Pause</string>
    <string name="startButtonLabel">Start</string>
</resources>

计时器
设置
你好,世界!
00:00:00
暂停
开始
活动\u主xml文件

<?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:background="#000000"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/timerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pauseButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="37dp"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:text="@string/timerVal" />
    <Button
        android:id="@+id/startButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="38dp"
        android:text="@string/startButtonLabel" />
    <Button
        android:id="@+id/pauseButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignBaseline="@+id/startButton"
        android:layout_alignBottom="@+id/startButton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="38dp"
        android:text="@string/pauseButtonLabel" />

    <Button
        android:id="@+id/buttonReset"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:text="Reset"
        android:layout_below="@+id/timerValue"
        android:layout_centerHorizontal="true"
        android:onClick="onButtonTapReset" />
</RelativeLayout>


我知道我的问题很长,但我喜欢详细说明。

当用户单击“取消”按钮时,视图的文本临时设置为00:00:000,因为第二个线程仍在后台通过以下代码进行更新:

entprivate Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};
你应该考虑在这段代码中点击取消按钮并相应动作。 要检查是否存在这种情况,一个快速的解决方案是在取消按钮的OnClick侦听器中将startTime更改为当前时间。 然后,时间应设置在“00:00:000”附近。
如果是这样的话,那么很容易找到一种让它完美工作的方法。

你能告诉我怎么做吗?我试着做你说的和其他事情,但我更困惑了:(你能帮我吗?谢谢!:)首先,将取消按钮侦听器更改为:public void onbuttontpreset(视图v){timeinmillides=SystemClock.uptimeMillis()-startTime;timerValue.setText(“00:00”);}看看是否有什么结果,尼科斯,我试过你说的,但它做同样的事情休息到00:00:00,但当我按下开始按钮,它会恢复到它停止的地方。有什么建议吗?请尝试将您的方法更改为public void onButtontPreset(视图v){timeinmillies=SystemClock.uptimeMillis()-startTime;customHandler.postDelayed(updateTimerThread,0);}