I am trying to show a alertdialog but i keep getting this error
01-24 21:52:45.640: E/AndroidRuntime(31119): FATAL EXCEPTION: UpdateThread
01-24 21:52:45.640: E/AndroidRuntime(31119): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-24 21:52:45.640: E/AndroidRuntime(31119): at android.os.Handler.<init>(Handler.java:121)
01-24 21:52:45.640: E/AndroidRuntime(31119): at android.app.Dialog.<init>(Dialog.java:100)
01-24 21:52:45.640: E/AndroidRuntime(31119): at android.app.AlertDialog.<init>(AlertDialog.java:96)
01-24 21:52:45.640: E/AndroidRuntime(31119): at android.app.AlertDialog$Builder.create(AlertDialog.java:891)
It is pointing to this line
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
isGameRunning = false;
gameStarted = false;
mEngine.stop();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("How To Play");
builder.setIcon(R.drawable.ic_launcher);
final AlertDialog alert = builder.create(); //this line
builder.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
alert.dismiss();
isGameRunning = true;
mEngine.start();
gameStarted = true;
}
});
return alert;
default:
return null;
}
}
It seems like, for some reason, the method
onCreateDialogis called from the UpdateThread of AndEngine (While it should be called from the UI Thread).Remember that when you show a dialog, you call
showDialog(int id)rather thanonCreateDialog(int id). After you callshowDialog, Android will callonCreateDialogon the UI thread when possible.In fact, I looked in showDialog info now and it is deprecated, it looks like there is a whole new way of creating dialogs. Read more here.