Skip to content Skip to sidebar Skip to footer

Button Onclick Listener Wont Call Alert Dialog

I'm attempting to prompt an AlertDialog whenever a button is pressed however I've only been able to successfully implement (using a tutorial) a successful AlertDialog on the first

Solution 1:

By calling these one after another:

button = (Button) findViewById(R.id.button1);button = (Button) findViewById(R.id.button2); // This obliterates the previous value of buttonbutton = (Button) findViewById(R.id.button3); // This obliterates the previous value of button

You are overwriting the previous variable. Either use different variables or do the work you want before overriding its value:

button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mOnClickListener);
//etc.

button = (Button) findViewById(R.id.button2);
button.setOnClickListener(mOnClickListener);
//etc.

As far as the error you see, you have defined an OnClickListener in your XML with:

android:onClick="onPopupBtClick"

But failed to write this method in your Activity...

publicvoidonPopupBtClick(View view) {
    // Do something
}

Solution 2:

Remove the onPopupBtClick off the xml views. It cannot find a method in your activity called onPopupBtClick and therefor is crashing

Also you cannot assign 3 different views to the same Button variable.

Solution 3:

publicclassMainActivityextendsActivity {
privateButton button1,button2,button3;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);

    /*
     * Implementation for dynamic OnClicklisteners
     *///      button1.setOnClickListener(new OnClickListener(){//          @Override//          public void onClick(View v) {//              openDialog(v);//          }//      });//      button2.setOnClickListener(new OnClickListener(){//          @Override//          public void onClick(View v) {//              openDialog(v);//          }//      });//      button3.setOnClickListener(new OnClickListener(){//          @Override//          public void onClick(View v) {//              openDialog(v);//          }//      });

}
publicvoidopenDialog(View v){
    AlertDialog.Builder alertDialogBuilder = newAlertDialog.Builder(MainActivity.this)
    .setTitle("Settings Menu")
    .setMessage("Delete Edit or Link?")
    .setCancelable(false)
    .setNeutralButton("Edit", newDialogInterface.OnClickListener() {
         publicvoidonClick(DialogInterface dialog,int id) {
             Toast.makeText(MainActivity.this, "Clicked Edit",Toast.LENGTH_LONG).show();
         }
    })
    .setPositiveButton("Link",newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog,int id) {
            Toast.makeText(MainActivity.this, "Clicked Link",Toast.LENGTH_LONG).show();
        }
    })    
    .setNegativeButton("Delete",newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog,int id) {

            dialog.cancel();
        }
     });

    AlertDialog alertDialog = alertDialogBuilder.create();

    alertDialog.show();
}        





<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#FFF"android:orientation="vertical" ><TextViewandroid:textAppearance="?android:textAppearanceLarge"android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="" /><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Core Device 1"android:onClick="openDialog" /><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Core Device 2"android:onClick="openDialog" /><Buttonandroid:id="@+id/button3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Core Device 3"android:onClick="openDialog" /></LinearLayout>

You can see the source at https://github.com/darussian/SimpleAndroidDialogExample

Post a Comment for "Button Onclick Listener Wont Call Alert Dialog"