简体   繁体   English

Android自定义对话框强制关闭

[英]Android custom dialog force close

I am implementing a custom dialog to form an about screen with a simple look and feel (which may become more complex once I get it working). 我正在实现一个自定义对话框,以形成一个简单的外观和感觉的屏幕(一旦我开始工作,它可能会变得更加复杂)。 I have a xml layout that loads some text and an icon from R. However when ever showDialog(int) is called I get a force close. 我有一个xml布局,从R加载一些文本和一个图标。然而,当调用showDialog(int)时,我得到一个力关闭。

I followed the instructions as per the dev guide: http://developer.android.com/intl/de/guide/topics/ui/dialogs.html 我按照开发指南按照说明操作: http//developer.android.com/intl/de/guide/topics/ui/dialogs.html

code is as follows: 代码如下:

 @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 is defined as private static final int DIAG_ABOUT = 0; DIAG_ABOUT定义为private static final int DIAG_ABOUT = 0;

The Log cat shows the following trace: 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)

Can some one point out where I have gone wrong, I am using SDK 1.5 有人可以指出我出错的地方,我使用的是SDK 1.5

You need to use your Activty as the Context for the Dialog not the Application . 您需要将Activty用作Dialog而不是ApplicationContext

Android is looking for a view to display the Dialog in and the Application does not have one but your Activity will. Android正在寻找一个视图来显示对话框,而Application没有一个,但你的Activity将会。 The Application is an object which ties your app together and has a Context as it knows about which locale you're running in and so on, but it relies on Activities to display things to the user. Application是一个将您的应用程序绑定在一起的对象,它具有一个Context因为它知道您正在运行的区域设置等等,但它依赖于Activities来向用户显示内容。

Replace: 更换:

dialog = new Dialog(getApplicationContext());

with: 有:

dialog = new Dialog(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM