2010-03-04 86 views
4

我正在實現一個自定義對話框,以簡單的外觀和感覺形成一個關於屏幕(一旦我獲得它可能會變得更加複雜)。我有一個xml佈局,從R加載一些文本和一個圖標。然而,當showDialog(int)被調用時,我得到一個關閉的力量。Android自定義對話框強制關閉

我按照說明按照dev的指南:http://developer.android.com/intl/de/guide/topics/ui/dialogs.html

代碼如下:

@Override 
protected Dialog onCreateDialog(int id) { 
    Dialog dialog; 
     switch(id) { 
     case DIAG_ABOUT: 
     dialog = new Dialog(getApplicationContext()); 
     dialog.setContentView(R.layout.aboutdialog); 
     dialog.setTitle(R.string.about_title); 
     dialog.setOwnerActivity(this); 
     break; 
     default: 
      dialog = null; 
     } 
     return dialog; 
} 

DIAG_ABOUT被定義爲private static final int DIAG_ABOUT = 0;

日誌貓顯示以下跟蹤:

03-04 11:37:08.760: ERROR/AndroidRuntime(726): Uncaught handler: thread main exiting due to uncaught exception 
03-04 11:37:08.780: ERROR/AndroidRuntime(726): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.ViewRoot.setView(ViewRoot.java:429) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.app.Dialog.show(Dialog.java:231) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.app.Activity.showDialog(Activity.java:2407) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at uk.me.cpearson.runningLate.Home.onOptionsItemSelected(Home.java:143) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.app.Activity.onMenuItemSelected(Activity.java:2085) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:820) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:519) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.View.onTouchEvent(View.java:3828) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.widget.TextView.onTouchEvent(TextView.java:6291) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.View.dispatchTouchEvent(View.java:3368) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1525) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.os.Looper.loop(Looper.java:123) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
03-04 11:37:08.780: ERROR/AndroidRuntime(726):  at dalvik.system.NativeStart.main(Native Method) 

可以這樣我有一點指出我出錯的地方,我正在使用SDK 1.5

回答

10

你需要使用你的Activty作爲ContextDialog而不是Application

Android正在尋找一個顯示對話框的視圖,並且Application沒有一個,但您的Activity會。 Application是一個將您的應用綁定在一起的對象,它具有一個Context,因爲它知道您正在運行哪些區域設置等,但它依賴於活動向用戶顯示內容。

替換:

dialog = new Dialog(getApplicationContext()); 

有:

dialog = new Dialog(this); 
+1

的Android文檔提供了錯誤的榜樣,有創造了很久以前的事了票,但這個問題仍然是不固定的! –