androidmessageboxandroid-applicationinfo

Adding a message box with Ok button inside an onCLickListener()


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);

Solution

  • 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