Question

I am making a program that has a timer in it. I have this code. The program is forced to close after the start button is clicked. but if i make >myHandler.postDelayed(updateTimerMethod, 0); a comment, the program will run, but the timer in the program will not. WHAT IS WRONG WITH MY CODE???

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)

manifest

<?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>

Was it helpful?

Solution

Change this line...

private Handler myHandler;

to...

private Handler myHandler = new Handler();

You forgot to initialize your muHandler object. Initialize it.

Update:

why did you put this in the following line inside your thread???

myHandler.postDelayed(this, 0);

Here, this means the current Acitivity's context. Comment out the above line as below...I think your problem will be solved.

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

Another Update:

Initialize your TextView name textTimer in onCreate() method as below...

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

When you are trying set text to that TextView, it can't find any TextView.

OTHER TIPS

Add:

myHandler = new Handler();

before the problematic row of code.

EDIT after stack trace added:

From the stack trace: Unable to find explicit activity class {com.example.reactiontime/com.example.reactiontime.ingame}; have you declared this activity in your AndroidManifest.xml?

Check if you declared the activity in you manifest file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top