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 =
getActivity when you use then no need to put new... such as
PendingIntent pi=new PendingIntent.getActivity(this,0,intent,0); //is wrong
user it as :
PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); //is Right code
here we remove new that provide new allocation but here it provide its allocation via getActivity in with in Activity(this).
getActivity() is implemented in the Fragment class.
See http://developer.android.com/reference/android/app/Fragment.html
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);
// 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();
// 4. Show the AlertDialog
dialog.show();
new AlertDialog.Builder() needs Context as input parameter. So try like
AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);
The constructor AlertDialog.Builder expects a Context
parameter. Context
is accessible from Activity
, Service
etc, since they all extend
Context
, and can be passed as this
.
The method getActivity()
is declared as others have mentiond in the Fragment
class.