1

I'm making my first android game, and I have the general things I wanted implemented with knowledge on making simple games in java and C#. For this I have the main class which extends Activity, then I have that create an object of a GameView class which extends SurfaceView, and setContentView to that class. Here's some of that code:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(gameView);

In my GameView class I have all the usual parts of a simple game, with a loop, and Sprite objects etc. This is all working great, until it comes to opening an AlertDialog to take text input from a user for saving the game. This is the part where i'm worried I've 'missed the point' of programming in android, because I've tried implementing solutions to this shown

here: Android AlertDialog inside AsyncTask

and here: How do I display an alert dialog on Android?

and here: http://www.androidsnippets.com/prompt-user-input-with-an-alertdialog

I get the error 'Can't create handler inside thread that has not called Looper.prepare()' the few times I've attempted these. I've gotten almost close to finishing this project and now I think I've missed fundamental android programming structure. The problem is I don't really know where I should be creating and showing the AlertDialog, should it be inside the main Activity, or the GameView SurfaceView. And if it is in the main activity, how do I exit out of the GameView back into the activity, and return some data for the next (view?) to use in an AlertDialog.

I'm sorry if this seems too vague, its just since I've tried to create an AlertDialog, its thrown off my understanding and I feel like I'm missing something.

So my question is, whats wrong with my structure, and more specifically, where do I put the AlertDialog; can I run it in some method or class that is called from my GameView class, or do I need to exit/end out of that class back into the main activity, and run it inside that?

private class myAsyncTask extends AsyncTask<String, Void, String> {

AlertDialog alertDialog;
protected void onPreExecute() 
{
    super.onPreExecute();
    alertDialog = new AlertDialog.Builder(this);
}
@Override
protected String doInBackground(String... params)
{       
       return null;
}
@Override
protected void onPostExecute(String result) 
{
       super.onPostExecute(result);

       alertDialog.setTitle("The Process");  
       //alertDialog.setIcon(R.drawable.success);
       alertDialog.setCanceledOnTouchOutside(false);
       alertDialog.setMessage("All done!");  
       alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
       new DialogInterface.OnClickListener() 
       {
                       public void onClick(DialogInterface dialog, int which) 
                       {

                       }
                               });
       alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() 
       {          
 @Override
 public void onDismiss(DialogInterface dialog) 
 {

 }
 });
 alertDialog.show();
 }

}
Community
  • 1
  • 1

1 Answers1

0

You have to show the Alert dailog inside onPostExecute method then only you can show. You can display Alert Dailog inside background running method.

Prefer below link:

Android AlertDialog inside AsyncTask

Updated

 Instead of AlertDialog alertDialog use
 AlertDialog.Builder alertDialogBuilder

Then After that create the Dialog

final AlertDialog alertDailog = alertDialogBuilder.create();

And at the end :

alertDailog.show();
Community
  • 1
  • 1
rupesh
  • 2,865
  • 4
  • 24
  • 50