0

I have made a package in com.example.expensemanager, named model. I added a new java class to it. In this class I have added AlertDialog.Builder method. When I pass getActivity() as an argument, Android Studio gives me an error. I don't know what to pass as an argument.

public void incomeDateInsert() {
    AlertDialog.Builder mydialog = new AlertDialog.Builder(getActivity());
}
Milo
  • 3,365
  • 9
  • 30
  • 44
Nageshwor Shah
  • 101
  • 1
  • 1
  • 8
  • What error is it giving? – Ananth Sep 30 '19 at 10:34
  • it give me a error cannot resolve method 'getActivity()' – Nageshwor Shah Sep 30 '19 at 10:36
  • Which class does your this class extend? Have a look at this thread : https://stackoverflow.com/questions/32527199/cannot-resolve-method-getactivity – Ananth Sep 30 '19 at 10:37
  • Possible duplicate of [Cannot resolve method getActivity()](https://stackoverflow.com/questions/32527199/cannot-resolve-method-getactivity) – Ananth Sep 30 '19 at 10:42
  • If you are calling this from a `Fragment`, `getActivity()` should work, if you are calling it from an `Activity`, you can use `ACTIVITYNAME.this` – ravi Sep 30 '19 at 12:27

5 Answers5

1

Try using getContext() instead of getActivity() because AlertDialog.Builder need context to create the dialog.

Ananth
  • 2,597
  • 1
  • 29
  • 39
1

getActivity() method is used in fragment to get the context of parent activity. It can be used as

  1. AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);

  2. AlertDialog.Builder builder = new AlertDialog.Builder(this);

Ananth
  • 2,597
  • 1
  • 29
  • 39
Shubham
  • 91
  • 1
  • 4
0

The thing is: AlertDialog.Builder() is something known as a Builder Pattern. It helps to create objects, in our case AlertDialog(s). Now when you want to view method signature of an Android method or function like setContentView() , you can do so by using Ctrl+Click or Command+Click. You will then be able to view what arguments or parameters to pass to the method.

For Builder however, you chain a series or methods on the builder itself, like:

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // getActivity() when you are on a Fragment, ActivityName.this when you are on an Activity

// 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 create()
AlertDialog dialog = builder.create();

// 4. Show the dialog
dialog.show()

Read more about Alert Dialogs on the official documentation.

ravi
  • 899
  • 8
  • 31
0

Try this:

AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);

getActivity() method is used in fragment to get the context of parent activity.

ravi
  • 899
  • 8
  • 31
0

Use RequireContext() instad of getContext(); because when context is null it will get error but not in case of RequireContext();

raj kavadia
  • 926
  • 1
  • 10
  • 30