user3367817
user3367817

Reputation: 45

cannot resolve symbol(context)

As still very young in android. am getting this error on this line in my code: new AlertDialog.Builder(context). The error I get is cannot resolve symbol(context).

please kindly help,

   case R.id.chk_clas1:
            //do stuff
            if (chk_clas1.isChecked()) {
                if(c1.equals("0")){
                adddate(txt_clas1);}
                clas="1";
                fdate=txt_clas1.getText().toString();

                new AlertDialog.Builder(context)
                  .setTitle("Delete entry")
                  .setMessage("Are you sure you want to delete this entry?")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();

            } else {
                txt_clas1.setText("");

            }

Upvotes: 1

Views: 10746

Answers (3)

Mohammed Sameer Ahmad
Mohammed Sameer Ahmad

Reputation: 437

Instead of context use yourActivity.this

because context is current class instance in my case

My Activity is MainActivity so I will use MainActivity.this

new AlertDialog.Builder(MainActivity.this)

Upvotes: 0

Amit Vaghela
Amit Vaghela

Reputation: 22965

Here, you use context as a variable but you have neither declared it, or initialised it, hence the error.

You could define it (and initialise at the same time)

 Context context = this;

since this refers to the current object instance of a class and Activity is a Context.

if you extends activity than use

new AlertDialog.Builder(this)

if you extends fragment than use

new AlertDialog.Builder(getActivity())

Upvotes: 2

Manmohan Kumar
Manmohan Kumar

Reputation: 434

try this, replace context with classname.this

Upvotes: 0

Related Questions