java - 为什么我添加到项目中的计时器类在单击按钮时没有启动?

标签 java android

在我的 MainActivity.java 项目中,我有:

public class MainActivity extends ActionBarActivity
{
    private static final int MY_DATA_CHECK_CODE = 0;
    public static MainActivity currentActivity;
    TextToSpeech mTts;
    private String targetURL;
    private String urlParameters;
    private Button btnClick;
    private String clicking = "clicked";
    private String[] ipaddresses = new String[]{
        "http://192.168.1.10:8098/?cmd=nothing"};
    private String iptouse = "";
    private TextView text;
    private boolean connectedtoipsuccess = false;
    private int counter = 0;
    private NotificationCompat.Builder mbuilder;
    private Timer timer = new Timer();

    private TextView text1, text2, text3;
    private long starttime = 0;

    private TimerCounter tc;

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

        tc = new TimerCounter();
    }
}

在 TimerCounter 类中,我有:

package com.test.webservertest;

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;

import java.util.Timer;

    public class TimerCounter 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);

                }
            });

        }

        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);
            }

        };
    }

这是资源按钮和 TextView 的 xml 文件代码:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="WEBSERVER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="50dp"
        android:textColor="#FFF"
        android:id="@+id/textView"/>

    <TextView
        android:id="@+id/textView1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:textSize="18sp"
        android:textColor="#ff0000"
        android:text="HttpURLConnection\nIP Based" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checking Connection.."
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="47dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Ip"
        android:id="@+id/checkipbutton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="61dp"/>

    <TextView
        android:id="@+id/timerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="40sp"
        android:textColor="#000000"
        android:layout_marginTop="90dp"
        android:text="@string/timerVal" />

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="170dp"
        android:text="startButtonLabel" />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pauseButtonLabel"
        android:layout_alignParentRight="true"
        android:layout_marginTop="170dp"/>


</RelativeLayout>

底部的 TextView 和两个按钮用于使用 TimerCounter。

然后我有文件 strings.xml:

<resources>
    <string name="app_name">WebServerTest</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string       name="title_activity_activity_motion_event">activity_motion_event</string>
    <string name="status1_string">Touch One Status</string>
    <string name="title_activity_activity__motion">Activity_Motion</string>
    <string name="status_string">Touch One Status</string>
    <string name="timerVal">00:00:00</string>
    <string name="pauseButtonLabel">Pause</string>
    <string name="startButtonLabel">Start</string>
</resources>

我没有收到任何错误或异常,但是当我运行程序并单击“开始”按钮时,没有任何反应。

最佳答案

有多种原因可以解释为什么您的应用程序未按预期运行。

首先,您的 TimerCounter 类扩展了 Activity。当您调用 tc = new TimerCounter() 时,您可能正在尝试启动 TimerCounter Activity 。如果你想实现这一点,你必须使用 Android Intent 并启动它

Intent i = new Intent(this, TimerCounter.class);
startActivity(i);

您还应该在manifest.xml中声明您的Activity:

<activity
        android:name="com.test.webservertest.TimerCounter"
        android:label="@string/app_name"
         />

您可能被两个 Activity 具有相同布局(R.layout.activity_main)这一事实所愚弄。因此,当您单击按钮时,您仍在 MainActivity 中,而不是在 TimerCounter Activity 中。

然后,为了优化您的计时器,您可以使用与计时器关联的 TimerTask。

关于java - 为什么我添加到项目中的计时器类在单击按钮时没有启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285516/

相关文章:

java - TableLayout 中的 TextView 数组

带圆角的 Android AlertDialog : rectangle seen below corners

android - 键盘上方有黑线

java - Android Studio : Could not find com. android.tools.build :gradle:3. 6.2 导入 Eclipse 项目期间

java - CRC Craking 知晓结果

java - AsyncTask doInBackground() 方法中的空指针异常

android - 无法使用firebase功能模拟器: INTERNAL ERROR

java - 使用正则表达式将字符串拆分为一些字符和转义序列

java - JSP 并行包含

java - Spring-Websocket:满足条件时向订阅者发送更新