views:

521

answers:

1

Hi,

i've build an AlertDialog which shows three items:

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}
...

now i want to display another dialog when the user selects the third item (item==2). My approach is the following:

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

This doesn't work :( An exception thrown:

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)

I've searched on stackoverflow/the inet but the solution that was meantioned there was to pass the applicationContext of the Activity to the Dialog (which i did in this case, i saved an reference of the activitys applicationContext in a private variable: myActivity.savedApplicationContext

+1  A: 

Fixed the problem.

Instead of savedApplicationContext = getApplicationContext();

i used savedApplicationContext = this;

now it works :-)

David