Android ALerDialog - 无法添加窗口异常
所以基本上我在 TabHost 中使用 TabGroupActivity,这样我就可以在一个选项卡内切换不同的活动。两天前我现在运行的代码没问题,没有任何例外。但在那段时间我正在翻转视图现在我用不同的活动更改所有视图。所以我的情况是这样的:
Main tab Activity -> opens Second activity -> which opens the third activity ,where the AlertDialog is.
我使用的代码是:
Button deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( getParent() )
.setTitle( "Warning" )
.setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
.setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
}
})
.show();
}
});
并且例外是:
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): FATAL EXCEPTION: main
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@463430d0 is not valid; is your activity running?
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.ViewRoot.setView(ViewRoot.java:509)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.Dialog.show(Dialog.java:241)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:74)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.View.performClick(View.java:2408)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.View$PerformClick.run(View.java:8817)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Handler.handleCallback(Handler.java:587)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Looper.loop(Looper.java:144)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at dalvik.system.NativeStart.main(Native Method)
我尝试使用 this
, MyCollecionId.this
,添加上下文广告等,但我无法让这些东西发挥作用。有什么想法可以解决这个问题吗?
So basically I'm using a TabGroupActivity inside my TabHost,so I can switch different activities inside of one tab.Two days before the code that I'm running right now was ok,without any exceptions.But in that time I was flipping views inside the tab.Now I change all views with different activities.So my situation is something like that:
Main tab Activity -> opens Second activity -> which opens the third activity ,where the AlertDialog is.
The code I'm using is that :
Button deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( getParent() )
.setTitle( "Warning" )
.setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
.setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
}
})
.show();
}
});
and the Exception is :
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): FATAL EXCEPTION: main
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@463430d0 is not valid; is your activity running?
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.ViewRoot.setView(ViewRoot.java:509)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.Dialog.show(Dialog.java:241)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:74)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.View.performClick(View.java:2408)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.view.View$PerformClick.run(View.java:8817)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Handler.handleCallback(Handler.java:587)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.os.Looper.loop(Looper.java:144)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): at dalvik.system.NativeStart.main(Native Method)
I tried with this
, MyCollecionId.this
, with adding Context ad etc.but I can't get the things to work.Any ideas how to solve that problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下,传递您正在运行的活动的上下文
try it out, pass your running activity's context
试试这个:
try this: