1

我试过下面的代码..

            btnRemoveItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Bundle b = new Bundle();
                    b.putLong("index", item.id);


                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder.setTitle("Are you sure you ?");
                    builder.setMessage("Are you suer you want to remove this item from the cart?");
                    builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }

                    });
                    builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    Dialog dd= builder.create();
                    dd.show();

                }
            });

但我收到以下错误..

08-11 19:18:50.968: E/AndroidRuntime(827): FATAL EXCEPTION: main
08-11 19:18:50.968: E/AndroidRuntime(827): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.view.ViewRoot.setView(ViewRoot.java:531)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.app.Dialog.show(Dialog.java:241)
08-11 19:18:50.968: E/AndroidRuntime(827):  at com.gand.metro.uis.SearchActivity$ItemsAdapter$1.onClick(SearchActivity.java:406)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.view.View.performClick(View.java:2485)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.view.View$PerformClick.run(View.java:9080)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.os.Handler.handleCallback(Handler.java:587)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:123)
08-11 19:18:50.968: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-11 19:18:50.968: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 19:18:50.968: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:507)
08-11 19:18:50.968: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-11 19:18:50.968: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-11 19:18:50.968: E/AndroidRuntime(827):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

AlertDialogs 只能使用 Activity 上下文创建,因此 getApplicationContext() 将不起作用。相反,将以下全局变量添加到您的 Activity 文件中:

Context mContext;

然后将以下内容添加到您的 onCreate() 中:

mContext = this;

现在,改变:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
于 2012-08-11T19:29:44.840 回答
0

当您应该使用活动上下文时,您正在使用 getApplicationContext() ,而是使用 SearchActivity.this

于 2012-08-11T19:33:41.230 回答