I would like to insert a time counter inside a game. If the time is 0, there would be an AlertDialog which tells the user the time is out, and goes back to the previous Activity. Here is the method (it is inside a class which extends a SurfaceView):
public void showTime(){
time--;
Log.i("GameView time", "" + time);
if (time <= 0){
Log.i("gameview time","time out");
gameTimer.setRunning(false);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this.getContext());
AlertDialog alert = alt_bld.create();
alert.setTitle("Time is out. You lose.");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
main.onBackPressed();
}});
alert.show();
}
}
The GameTimer class is a Thread:
public class GameTimer extends Thread{
private GameView gameView;
private boolean run;
public GameTimer(GameView gameView){
this.gameView = gameView;
}
public void setRunning(boolean value){
this.run = value;
}
public void run(){
Looper.prepare();
while (run){
try {
gameView.showTime();
sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
}
Looper.loop();
}
}
The AlertDialog appears, but the app crashes, with the message: Only the original thread that created a view hierarchy can touch views. But this is the thread which created… Where’s the problem?
Do this using Handler or Use
The error itself tells the whole story.
And if you are not in Activity/ View’s Parent class then use some callback Mechnasim.
This will help you to resolve your issue.
Cheers.