Search code examples
javaandroidandroid-alertdialogandroid-actionbaractivity

Create AlertDialog in ActionBarActivity


I'm rebuilding my MainActivity that previously extended Activity. Now that it extends ActionBarActivity (rebuild app from scratch), i'm having trouble building an AlertDialog. This is how I did it previously:

final AlertDialog mDialog = new AlertDialog.Builder(getActivity())
            .setTitle("Change PIN")
            .setView(content)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    currentPIN = mEditText.getEditableText().toString();
                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    }).create();

now that my Activity extends ActionBarActivity, how do I do this? My specific problem is that now the method getActivity() is undefined for the type MainActivity. I found this other question that was answered, but doesn't make much sense to me:

Create dialog in ActionBarActivity

Help por favor


Solution

  • you can use MainActivity.this instead of getactivity()