1

我已经构建了一个 AlertDialog ,它显示了三个项目:

AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
...
builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        if(item==0){ //do stuff}
                                    else if(item==1){//do other stuff}
                                    else if(item==2){//show other dialog}
...

现在我想在用户选择第三项(item==2)时显示另一个对话框。我的方法如下:

Dialog otherDialog = new Dialog(myActivity.savedApplicationContext);
otherDialog.setContentView(R.layout.other_layout);
otherDialog.setTitle(getString(R.string.update_dialog));
dialog.dismiss(); //dismiss old dialog
otherDialog.show(); 

这不起作用:(抛出异常:

06-03 12:21:32.684: ERROR/AndroidRuntime(507): Uncaught handler: thread main exiting due to uncaught exception
06-03 12:21:32.834: ERROR/AndroidRuntime(507): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.view.ViewRoot.setView(ViewRoot.java:472)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.app.Dialog.show(Dialog.java:239)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at com.dapr.altfuelloc.activity.DetailsActivity$1$1.onClick(DetailsActivity.java:96)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.widget.ListView.performItemClick(ListView.java:3285)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.os.Handler.handleCallback(Handler.java:587)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.os.Looper.loop(Looper.java:123)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 12:21:32.834: ERROR/AndroidRuntime(507):     at dalvik.system.NativeStart.main(Native Method)

我已经在 stackoverflow/inet 上进行了搜索,但是这里的解决方案是将 Activity 的 applicationContext 传递给 Dialog(在这种情况下,我将活动 applicationContext 的引用保存在私有变量中:myActivity.保存的应用程序上下文

4

1 回答 1

1

修复了问题。

而不是 savedApplicationContext = getApplicationContext();

我使用了 savedApplicationContext = this;

现在它可以工作了:-)

于 2010-06-03T12:47:06.530 回答