• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps mUpdateTimeTask UI help

wertyu

Newbie
I'm trying to create a basic sample Stop Watch type application which basically displays a timer. It can then be stopped and started. I'm using the mUpdateTimeTask to do this. However my app just crashed when I run it. Below is the code; can anyone help?


public
class TimeWatch extends Activity {
/** Called when the activity is first created. */
private Button btnStart;
private Button btnStop;


private TextView mTimeLabel;

private long mStartTime;

final Handler mHandler = new Handler();


private
Runnable mUpdateTimeTask = new Runnable()
{
publicvoid run()
{
finallong start = mStartTime;

long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;

if (seconds < 10) {
time.setText("" + minutes + ":0" + seconds);
}
else {
time.setText("" + minutes + ":" + seconds);
}
mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000));
mHandler.postDelayed(mUpdateTimeTask, 100);
}};

private OnClickListener mClickListener = new OnClickListener() {

publicvoid onClick(View v) {
if (v.getId() == R.id.btnCalculate) {
if (mStartTime == 0L) {
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
else{
mHandler.removeCallbacks(mUpdateTimeTask);
}
}
}};




publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Access the various widgets by their id in R.java
time= (TextView) findViewById(R.id.time);
btnStart.setOnClickListener(mClickListener);
btnStop.setOnClickListener(mClickListener);

}

}

 
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2454
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2470
ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119
ActivityThread$H.handleMessage(Message) line: 1821
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4310
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 860
ZygoteInit.main(String[]) line: 618
NativeStart.main(String[]) line: not available [native method]
 
#1, don't recreate the wheel > android.widget.Chronometer. It may not be exactly what you want ... but its self contained.

#2, Your missing quite a bit from your onCreate() method.
#2.1, Your not setting the content view. Which means no layout has been inflated. All R.layout.xxx will return null
#2.2, Both button references are never initialized so they are both null as well.

#3, the stack trace you posted from the exception, seems incomplete.
 
hey wertyu i want the executed code for this problem. can u post it here plz, it would be very helpful to me..how that btncalclate and time are are present in Resource file..
 
Back
Top Bottom