PDA

View Full Version : Force Close Help - My First App



Crimson Major
01-21-2011, 01:41 PM
Hello all,

Im new to android dev, and am getting a force close on my first app. Its supposed to start a timer when one button is clicked and stop it when another is clicked. Its probably a basic rookie mistake, but I cant for the life of me work it out. Any help would be much appreciated!

Hello,

Im new to android dev, and am getting a force close on my first application and I cant figure it out for the life of me. Hope someone can spot it!

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

public class inputTest extends Activity {

private Handler mHandler = new Handler();
public long mStartTime;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

buttonStart.setOnClickListener(mStartListener);
buttonStop.setOnClickListener(mStopListener);

}

Button buttonStart = (Button) findViewById(R.id.button_start);
Button buttonStop = (Button) findViewById(R.id.button_stop);
TextView timer = (TextView)findViewById(R.id.timer);

OnClickListener mStartListener = new OnClickListener () {
public void onClick (View v) {
if (mStartTime == 0L){
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
};

OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(mUpdateTimeTask);
}
};


private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;

if (seconds < 10) {
timer.setText("" + minutes + ":0" + seconds);
} else {
timer.setText("" + minutes + ":" + seconds);
}

mHandler.postAtTime(this,
start + (((minutes * 60) + seconds + 1) * 1000));
}
};

}

Crimson Major
01-23-2011, 09:56 AM
Anyone got any ideas?