Android上的计时器

时间:2014-03-03 17:06:16

标签: android timer nullpointerexception handler delay

我正在制作一个有计时器的程序。我有这个代码。单击开始按钮后,程序将被强制关闭。但如果我做了> myHandler.postDelayed(updateTimerMethod,0); 注释,程序将运行,但程序中的计时器不会。什么是我的代码错误???

public class MainActivity extends Activity {
private TextView textTimer;
private Handler myHandler;
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
private long startTime = 0L;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button start = (Button)findViewById(R.id.Start);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent nextScreen = new Intent(getApplicationContext(),ingame.class);
            startActivity(nextScreen);
            startTime = SystemClock.uptimeMillis();
    myHandler.postDelayed(updateTimerMethod, 0); // if i make this line of code a comment, the program will run but without a timer
        }
    });
}

    private Runnable updateTimerMethod = new Runnable() {

        @Override
        public void run() {
            timeInMillies = SystemClock.uptimeMillis() - startTime;
            finalTime = timeSwap + timeInMillies;

            int seconds = (int) (finalTime / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            int milliseconds = (int) (finalTime % 1000);
            textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
            myHandler.postDelayed(this, 0);
        }
    };

}

UPDATED logcat

03-04 01:53:28.103: E/AndroidRuntime(2191): FATAL EXCEPTION: main
03-04 01:53:28.103: E/AndroidRuntime(2191): java.lang.NullPointerException
03-04 01:53:28.103: E/AndroidRuntime(2191):     at com.example.reactiontime.MainActivity$1.run(MainActivity.java:50)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at android.os.Handler.handleCallback(Handler.java:587)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at android.os.Looper.loop(Looper.java:123)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at java.lang.reflect.Method.invoke(Method.java:521)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-04 01:53:28.103: E/AndroidRuntime(2191):     at dalvik.system.NativeStart.main(Native Method)

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.reactiontime"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.reactiontime.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ingame"></activity>
</application>

2 个答案:

答案 0 :(得分:1)

添加:

myHandler = new Handler();
在有问题的代码行之前

添加堆栈跟踪后编辑:

从堆栈跟踪:无法找到显式活动类{com.example.reactiontime / com.example.reactiontime.ingame};你有没有在AndroidManifest.xml中声明这个活动?

检查您是否在清单文件中声明了活动。

答案 1 :(得分:1)

更改此行...

private Handler myHandler;

为...

private Handler myHandler = new Handler();

您忘记初始化muHandler对象。初始化它。

<强>更新

为什么你把this放在你的帖子里面的下一行???

myHandler.postDelayed(this, 0);

此处,this表示当前Acitivity的上下文。将以上内容评论如下......我认为您的问题将得到解决。

private Runnable updateTimerMethod = new Runnable() {

    @Override
    public void run() {
        timeInMillies = SystemClock.uptimeMillis() - startTime;
        finalTime = timeSwap + timeInMillies;

        int seconds = (int) (finalTime / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;
        int milliseconds = (int) (finalTime % 1000);
        textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
        //myHandler.postDelayed(this, 0);
    }
};

另一个更新:

TextView方法中初始化您的textTimer名称onCreate(),如下所示...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button start = (Button)findViewById(R.id.Start);

    //initializ as below
    textTimer = (TextView)findViewById(R.id.your_text_view);
}

当您尝试将文字设置为TextView时,它找不到任何TextView