3

I am trying to add an Ok button by following a post and tried that code but somehow it shows me an error

Builder (android.content.Context) in builder cannot be applied to (anonymous android.view.View.onClickListener)

Here is my code

submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (Arrays.asList(input).contains("")){
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("You still have unanswered questions. Please go back");
                alert.setTitle("Notice");
                alert.setPositiveButton("OK",
                        new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which){

                            }
                        });
            }else {
                Intent in = new Intent(getApplicationContext(),gcfResult.class);
                startActivity(in);


            }
        }
    });

The error is on this keyword at below line

AlertDialog.Builder alert = new AlertDialog.Builder(this);
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
desperateStudent
  • 81
  • 1
  • 1
  • 8
  • 1
    this references to View.onClickListener you probably using this code in activity lets say MyActivity you should create builder like this"new AlertDialog.Builder(MyActivity.this)" – Farid Jan 14 '17 at 14:03

1 Answers1

2

you need to use

 AlertDialog.Builder alert = new AlertDialog.Builder(YourActivityname.this);

because new View.OnClickListener() { is an anonymous class and this here points to anonymous class not to your Activity

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68