Android自定义对话框强制关闭

时间:2010-03-04 11:49:26

标签: android dialog

我正在实现一个自定义对话框,以形成一个简单的外观和感觉的屏幕(一旦我开始工作,它可能会变得更加复杂)。我有一个xml布局,可以从R加载一些文本和一个图标。但是当调用showDialog(int)时,我会强制关闭。

我按照开发指南按照说明操作: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;

Log cat显示以下跟踪:

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

1 个答案:

答案 0 :(得分:10)

您需要将Activty用作Context Dialog而不是Application

Android正在寻找一个显示对话框的视图,而Application除了Activity之外没有{。}}。 Application是一个将您的应用绑定在一起并且有一个Context的对象,因为它知道您正在运行的区域设置等等,但它依赖于活动来向用户显示内容。 / p>

替换:

dialog = new Dialog(getApplicationContext());

使用:

dialog = new Dialog(this);