2

I create a custom view which showing the simple game

I set the custom view in the MainActivity

setContentView(new CustomView())

In this custom view, only have few ball and the timer

When a ball touch another ball. The timer will stop and show the result.

I don't know how to show the result in a better way. So i tried to create a dialog to show the result.

This code is write in the CustomView class

if (ballIsTouch) {
            AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
            alertDialog.setTitle("Result");
            alertDialog.setMessage(point);
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
            alertDialog.show();
        }

However, the page is freezed. The dialog haven't show.

user2520217
  • 319
  • 1
  • 4
  • 17

1 Answers1

0
  • create() - is a final step -when you use builder pattern to create dialog u first set in chain all variables and then create dialog - and show so it should be

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
        alertDialogBuilder.setTitle("Result");
        alertDialogBuilder.setMessage(point);
        alertDialogBuilder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    

or

        alertDialogBuilder.setTitle("Result")
          .setMessage(point)
          .setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

ensure you getContext() is valid (should be activity, service context)** in activity use this or Myactivity.this in interface/callback in fragment getActivity()

**see Dialog creation context

Community
  • 1
  • 1
ceph3us
  • 7,326
  • 3
  • 36
  • 43