单击图像按钮时如何显示警告对话框(使用ok按钮)?

时间:2014-08-15 06:58:43

标签: java android

我想在点击AlertDialog时显示image_button。 AlertDialog将显示带有OK按钮的消息。单击“确定”时,需要关闭该对话框。我尝试了以下代码并抛出exception。请告诉我这里我做错了什么。

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" />

生成错误

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)

2 个答案:

答案 0 :(得分:1)

改变你的

 new AlertDialog.Builder(getApplicationContext());

 new AlertDialog.Builder(this);

希望它有所帮助:)

答案 1 :(得分:1)

我认为问题出在getApplicationContext()上。 试试这段代码:

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