I have an extended dialog class that I want to show for 3 seconds then disappear. This works great the first 2 times it's called, but then it crashes my app after that. Admittedly, I'm not the best with threads and I think that's where my problem might be. As you can see from the code below (commented out section), I tried using a cancel event to kill the thread that is spawned, but that makes it crash the first time it's run. I've also tried doing all of this on the parent class' UI thread which yields the same result as this (crashes after 3 times displaying the dialog).
import java.util.Timer;
import java.util.TimerTask;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
public class HandResults extends Dialog implements DialogInterface {
HandResults hr;
Timer myTimer;
Handler hand = new Handler();
Thread t;
public HandResults(Context context) {
super(context);
setContentView(R.layout.handresults);
hr = this;
/*
this.setOnCancelListener(new OnCancelListener(){
public void onCancel(DialogInterface dialog) {
t.destroy();
}
});
*/
}
public void showHands(){
this.show();
myTimer = null;
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 3000);
}
private void TimerMethod()
{
t = new Thread(){
public void run(){
hand.post(Timer_Tick);
}
};
t.start();
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
hr.cancel();
}
};
}