Reputation: 2320
I am trying to create a simple alert dialogue and I am following the steps from this link
However,I am getting the error: getActivity() can't be resolved
After some search I understood that getActivity()
can be user defined, but I am not sure what should I make it do.
Here is what I have:
public void about(View v1) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();//go to the previous activity
}
});
// Set other dialog properties
builder.setMessage(R.string.myName);
// Create the AlertDialog
AlertDialog dialog = builder.create();}
getActivity()
be used for?Upvotes: 0
Views: 46
Reputation: 1
// in a fragment
new AlertDialog.Builder(getContext())
.setTitle("Alert title")
.setMessage("Alert Message")
.setCancelable(false)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
Upvotes: 0
Reputation: 330
getActivity() is fragments method to get Activity. If u are in activity u just need to replace this by YourActivityClassName.this
Upvotes: 2