1

in my application i am using a canvas and some objects on that.this implementation is done in a separate thread called mythread.in a particular scenario i want to display an alert dialog on my screen.first itried to implement this by using the given below function.

  public void StopChecking()
{
    if(stopgameflag==true)
    {
        context=MyActivity.this;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Game Over !!!")
                .setCancelable(false)
                .setPositiveButton("Play Again",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                setContentView(R.layout.main);
                                dialog.cancel();
                            }
                        })
                .setNegativeButton("Exit",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                dialog.cancel();

                            }
                        });


        Log.d(TAG, "Stopping...oooooooooooooooooooooooooooooo");

    }
}

this is a function written in the MyActivity class(main activity).but it is crashing. somebody said that handlers is the best method to do that.i have searched for the same.but i could not understand anything..
can anyone tell me that how can i implement handlers to display an alert dialog...

andro-girl
  • 7,989
  • 22
  • 71
  • 94
  • If you want help with the crash, then post the logcat stack trace – skynet Dec 06 '11 at 05:31
  • sure Craigy.... 12-06 11:22:21.263: ERROR/AndroidRuntime(366): java.lang.IllegalStateException: System services not available to Activities before onCreate() ..this is the error in logcat – andro-girl Dec 06 '11 at 05:53
  • Remove the code that you put above ur onCreate() to below – xDragonZ Dec 06 '11 at 06:12

1 Answers1

1

Call this function after game over condition check

if(gameover) {
    StopChecking();
}


public void StopChecking() {

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
    alertDialog.setTitle("*GameOver*");        
    alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int which) {                                       

             return;  
             }  
         });  
         alertDialog.setButton2("Play Again", new DialogInterface.OnClickListener() {  
             public void onClick(DialogInterface dialog, int which) {

                 // start  game again                     
                 return;  
             }  
         });
         alertDialog.show();    
}
Dhasneem
  • 4,037
  • 4
  • 33
  • 47
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • i tried this..but my app crashed and the error is given ....12-06 11:33:29.384: ERROR/AndroidRuntime(404): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – andro-girl Dec 06 '11 at 06:04
  • i just wanted to display an alert dialog in a particular scenario...actually it is a game..when the game is over i have to display an alert dialog with two options "play again" and "exit".my game is written in thread called mythread. – andro-girl Dec 06 '11 at 06:36
  • checking whether the game is over or not is not in the same class...stopchecking() function is wwritten in my main activity class and game over is checking in another class...that is running on a thread... – andro-girl Dec 06 '11 at 07:18
  • this function will work for the creation of alert dialog..but remaining things i dont know.. – RajaReddy PolamReddy Dec 06 '11 at 07:28