I'm very new to android and I'm following this example.
The code says we need to do these steps to get an dialog box:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
But where does the getActivity()
method is defined?
I can't find that method.
getActivity(); is a method of android Fragment, if you want to show dialog in your activity, just pass
this
of your activity instead ofgetActivity()
.I had exactly the same problem and finally I found what I actually suspected... Simply add:
...and voila. There it is. Wonder why this isn't stated in the original example!?
getActivity() is implemented in the Fragment class.
See http://developer.android.com/reference/android/app/Fragment.html
new AlertDialog.Builder() needs Context as input parameter. So try like
// 1. Instantiate an AlertDialog.Builder with its constructor
// 2. Chain together various setter methods to set the dialog characteristics
// 3. Get the AlertDialog from create()
// 4. Show the AlertDialog
Update android API level 23.
Use
getContext()
instead ofgetActivity()
.