2010-06-03 51 views
1

我建立一個AlertDialog這說明三個項目:顯示自定義對話框一個Android 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} 
... 

現在我想,當用戶選擇第三項(項目顯示另一個對話框== 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) 

我已經搜查計算器/在INET但被meantioned解決方案有通過活動到對話框的applicationContext(我在此情況下那樣,我保存在一個專用變量的所述activitys的applicationContext的參考:myActivity.savedApplicationContext

回答

1

固定的問題

而不是 savedApplicationContext = getApplicationContext();我使用 savedApplicationContext = this;

現在它工作:-)