Sridhar Iyer
Sridhar Iyer

Reputation: 29

How to display an alert dialog (with ok button) when an image button is clicked?

I want to display an AlertDialog when an image_button is clicked. The AlertDialog will show a message with a OK button. The dialog needs to be dismissed when OK is clicked. I have tried the following code and it throws an exception. Please tell me what am I doing wrong here.

public void showHelp(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setPositiveButton("helpMessafe", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  dialog.dismiss();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

XML :

<ImageButton
    android:id="@+id/popupbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="18dp"
    android:layout_toLeftOf="@+id/towing1"
    android:layout_toRightOf="@+id/textView1"
    android:contentDescription="@string/help_me_choose"
    android:onClick="showHelp"
    android:src="@drawable/help_icon" />

Error generated :

08-15 06:49:37.834: E/AndroidRuntime(1898): FATAL EXCEPTION: main
08-15 06:49:37.834: E/AndroidRuntime(1898): java.lang.IllegalStateException: Could 
not execute method of the activity
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.View$1.onClick(View.java:3599)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.View.performClick(View.java:4204)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.View$PerformClick.run(View.java:17355)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.os.Handler.handleCallback(Handler.java:725)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.os.Handler.dispatchMessage(Handler.java:92)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.os.Looper.loop(Looper.java:137)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.app.ActivityThread.main(ActivityThread.java:5041)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
java.lang.reflect.Method.invokeNative(Native Method)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
java.lang.reflect.Method.invoke(Method.java:511)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
dalvik.system.NativeStart.main(Native Method)
08-15 06:49:37.834: E/AndroidRuntime(1898): Caused by: 
java.lang.reflect.InvocationTargetException
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
java.lang.reflect.Method.invokeNative(Native Method)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
java.lang.reflect.Method.invoke(Method.java:511)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.View$1.onClick(View.java:3594)
08-15 06:49:37.834: E/AndroidRuntime(1898):     ... 11 more
08-15 06:49:37.834: E/AndroidRuntime(1898): Caused by: 
android.view.WindowManager$BadTokenException: Unable to add window -- 
token null is not for an application
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
android.app.Dialog.show(Dialog.java:281)
08-15 06:49:37.834: E/AndroidRuntime(1898):     at 
com.sri.myapp.GetInfo.showHelp(GetInfo.java:163)

Upvotes: 0

Views: 460

Answers (2)

Reuven Chacha
Reuven Chacha

Reputation: 889

I think the problem is with getApplicationContext(). try this code:

private void showYourMassege(String msassege) {

    AlertDialog.Builder builder = new AlertDialog.Builder(youActivityName.this);
    builder.setTitle("helpMessage");
    builder.setPositiveButton("OK", null); //the OK as positive button
    // The message
    builder.setMessage(msassege);
    // Create the alert dialog and display it
    AlertDialog theAlertDialog = builder.create();
    theAlertDialog.show(); 
}

Upvotes: 1

Spurdow
Spurdow

Reputation: 2043

change your

 new AlertDialog.Builder(getApplicationContext());

to

 new AlertDialog.Builder(this);

hope it helps :)

Upvotes: 1

Related Questions