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

        };
    }

这是资源按钮和文本视图的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>

底部的文本视图和两个按钮用于使用计时器计数器。
然后是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>

我没有收到任何ERORR或异常,但当我运行我的程序并单击开始按钮时,什么也不会发生。

最佳答案

有不同的原因可以解释为什么您的应用程序不能按预期运行。
首先,TimerCounter类扩展活动。当调用tc=new timercounter()时,可能正在尝试启动timercounter活动。如果你想达到这个目的,你必须使用android的意图并启动它

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

您还应该在manifest.xml中声明您的活动:
<activity
        android:name="com.test.webservertest.TimerCounter"
        android:label="@string/app_name"
         />

你可能被两个活动具有相同的布局(r.layout.activity_main)这一事实所愚弄。所以当你点击按钮时,你仍然在你的主活动中,而不是在TimerCounter活动中。
然后,要优化计时器,可以使用与计时器关联的timer任务。

10-08 17:21