-2

I want the alert dialog when clicking on the positive button to show toast, but instead, It shows an erorr!

the code

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Create new ?")
            .setMessage(" Do you want to create new msg ?!")
            .setPositiveButton("Yes ? ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(this," Ready to Create New Msg",Toast.LENGTH_LONG).show();

                }
            });

the error msg

Error:(38, 30) error: no suitable method found for makeText(<anonymous 
OnClickListener>,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to 
Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to 
Context)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

Use YourActivity.this or getApplicationContext() instead of just this in the first parameter of makeText

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

You must use Context correctly. Instead of this, use dialog.getContext() or NameOfYourActivity.this when creating the Toast.

Gilad Eshkoli
  • 1,253
  • 11
  • 27