1

我已将我的活动放在子视图中,现在我无法显示来自我的活动和适配器的对话框。在我的 logCat 中,我得到了

04-11 12:39:59.823: E/AndroidRuntime(12831): FATAL EXCEPTION: main
04-11 12:39:59.823: E/AndroidRuntime(12831): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41971f18 is not valid; is your activity running?
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.Window$LocalWindowManager.addView(Window.java:537)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.Dialog.show(Dialog.java:278)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.myapp.functions.DownloadsDetailsAdapter$1$2.run(DownloadsDetailsAdapter.java:148)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.Activity.runOnUiThread(Activity.java:4170)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.myapp.functions.DownloadsDetailsAdapter$1.onClick(DownloadsDetailsAdapter.java:139)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.View.performClick(View.java:3511)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.view.View$PerformClick.run(View.java:14105)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.os.Looper.loop(Looper.java:137)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at android.app.ActivityThread.main(ActivityThread.java:4440)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 12:39:59.823: E/AndroidRuntime(12831):    at dalvik.system.NativeStart.main(Native Method)

这是我如何尝试显示对话框的示例。

AlertDialog.Builder builder = new AlertDialog.Builder(
                            activity);
                    builder.setMessage(R.string.are_you_sure)
                            .setPositiveButton(R.string.yes,
                                    dialogClickListener)
                            .setNegativeButton(R.string.no, dialogClickListener)
                            .show();

注意:在我进行更改之前,此代码正在运行。我觉得我应该在新的可运行文件中运行它,但如果我必须这样做,有人可以告诉我该怎么做吗?

4

3 回答 3

8

我也面临同样的问题。我为此使用了标签栏。只需使用getParent()而不是youractivity.this.

我希望这将有所帮助。

于 2013-06-04T07:05:54.710 回答
1

activity对象必须是当前显示在屏幕上的活动。如果它已经暂停或尚未构建显示,则会出现此错误。

于 2013-04-11T11:12:07.523 回答
0

在这里查看 ActivityGroup 的答案。

对于 ActivityGroup 的子 Activity,当我们在 PrompDialog 等子 Activity 中使用 Context 时,我们无法确定它是否始终存在并存在。例如:

mPromptDialog = new LoginPromptDialog(this);//this is used as Context
//But when we use this context it may be destroyed.
//So this is the parent instance
mPromptDialog = new LoginPromptDialog(this.getParent());

偏好: http ://www.cnblogs.com/kaima/archive/2011/08/04/2127813.html

于 2013-09-25T02:10:12.400 回答