Home > Software engineering >  why alertDialog.setButton onclickListner show error that no suitable method found
why alertDialog.setButton onclickListner show error that no suitable method found

Time:02-03

MainActivity

package com.example.dialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlertDialog alertDialog= new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Term and Conditons");
        alertDialog.setIcon(R.drawable.baseline_info_24);
alertDialog.setMessage("have you read all term and condition");
alertDialog.setButton("yes, I've read",new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "Tes, you can proceed now..", Toast.LENGTH_SHORT).show();

    }
});
alertDialog.show();
    }
}

Error

C:\Users\anonhake\AndroidStudioProjects\dialog\app\src\main\java\com\example\dialog\MainActivity.java:20: error: no suitable method found for setButton(String,<anonymous OnClickListener>)
alertDialog.setButton("yes, I've read",new DialogInterface.OnClickListener(){
           ^
    method AlertDialog.setButton(int,CharSequence,Message) is not applicable
      (actual and formal argument lists differ in length)
    method AlertDialog.setButton(int,CharSequence,OnClickListener) is not applicable
      (actual and formal argument lists differ in length)
    method AlertDialog.setButton(int,CharSequence,Drawable,OnClickListener) is not applicable
      (actual and formal argument lists differ in length)

CodePudding user response:

don't build your dialog in first line (create() call), use AlertDialog.Builder instance, set "possitive" and "negative" buttons on this (also title, message etc.), then call create() or even simpler show()

check out sample HERE

// initial builder and msg setup
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Close app?").setTitle("Exit");

// can't close dialog with e.g. back button, must be picked on of buttons
builder.setCancelable(false); 

// buttons setup
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int id) {  
        finish(); // closing Activity
    }  
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int id) {
    
    }  
});
builder.show();

dialog will close automatically when any button clicked, the one with "Yes" will close whole Activity, "No" is empty and you will stay in (looks like empty) Activity

CodePudding user response:

As per error , it looks like method with parameter types that you are using is not there . So basically you have three choice to use

 1. setButton(int,CharSequence,Message)
 2. setButton(int,CharSequence,OnClickListener)
 3. setButton(int,CharSequence,Drawable,OnClickListener)
  • Related