Csteele5
Csteele5

Reputation: 1292

Android Cannot resolve AlertDialog.setPositiveButton()

So I'm trying to get my application to show an editable text-field in a dialog. Once you've entered your desired text, you can hit 'OK' to use that text to go to the next view and do something based on that text.

So this is what my code looks like.

public void onClick(View v){

    final EditText input = new EditText(MainActivity.this);
    final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

    alertDialog.setTitle("New Query");
    alertDialog.setView(input);
    alertDialog.setPositiveButton("Fire Query!", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton){
                    TableView.putExtra("query", input.getText());
                    startActivity(TableView);
        }
    });

     alertDialog.show();
}

However, the compiler is telling me that it can't resolve setPositiveButton() and in fact when I look look in the code-complete box, it indeed does not show even though it is listed for the builder on the Android documentation

Any ideas? I should mention that everything but the setPositiveButton is working. I just can't do anything with a dialog with a title and an editText field.

Upvotes: 0

Views: 1009

Answers (1)

Mattias Isegran Bergander
Mattias Isegran Bergander

Reputation: 11909

You are not calling it on the builder. Move the create() call to later on (and change the type to the Builder).

Upvotes: 2

Related Questions