我正在使用适用于 Android 的 Xamarin,并且在 AlertDialog.Builder.Show() 方法中遇到此错误,但它仅发生在某些三星(使用 Android 7.0)设备上,我们尝试了其他一些设备,但此问题并未出现不会发生。我只是在使用该应用程序一段时间后才得到它。堆栈跟踪如下
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0
at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <7802aa64ad574c33adca332a3fa9706a>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <7802aa64ad574c33adca332a3fa9706a>:0
at Android.App.AlertDialog+Builder.Show () [0x0000a] in <dc51acef1f304f0dab449a7fc6039799>:0
at Prizma.Controls.Common.BindingComboBox.ShowDialog () [0x00062] in C:\TeamProjects\PrizmaProject\Main\MobileSales.iOS\Prizma.Controls.iOS\Common\BindingComboBox.cs:408
--- End of managed Android.Views.WindowManagerBadTokenException stack trace ---
android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@cb70704 has already been added
at android.view.ViewRootImpl.setView(ViewRootImpl.java:902)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
at android.app.Dialog.show(Dialog.java:404)
at android.app.AlertDialog$Builder.show(AlertDialog.java:1136)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11185)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
我的代码是这样的
try
{
AlertDialog.Builder a = new AlertDialog.Builder(MainActivity);
a.SetTitle("Select");
a.SetAdapter(_Adapter, new EventHandler<DialogClickEventArgs>(ClosedDialog));
a.Create();
a.Show();
}
catch (Exception ex)
{
//Exception code
}
我将AlertDialog.Builder Show()方法放在Try Catch中,但是在我第一次得到异常后,我每次都会得到它。
一些备注
请帮助我,我已经被这个问题困扰了好几个星期,而我的拥有三星设备的客户快要杀了我:)
提前致谢。
问候
艾伦
您正在向死活动添加对话框,因此在显示对话框之前,您应该通过以下方式检查活动是否已完成:
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.app_name);
builder.setMessage(msg);
AlertDialog alert = builder.create();
//To check if activity is finished
if (!((Activity)ctx).isFinishing()) {
alert.show();
}
在片段中像这样显示对话框:
use if (!IsFinishing) in an activity
if (!((Activity)Context).IsFinishing)
{
try
{
AlertDialog.Builder a = new AlertDialog.Builder(MainActivity);
a.SetTitle("Select");
a.SetAdapter(_Adapter, new EventHandler<DialogClickEventArgs>(ClosedDialog));
a.Create();
a.Show();
}
catch (Exception ex)
{
//Exception code
}
}
try
{
RunOnUiThread (() => {
AlertDialog.Builder a = new AlertDialog.Builder(MainActivity);
a.SetTitle("Select");
a.SetAdapter(_Adapter, new EventHandler<DialogClickEventArgs>(ClosedDialog));
a.Create();
a.Show();
});
}
catch (Exception ex)
{
//Exception code
}
我知道这个问题发布已有六年了。但这似乎是三星设备上牛轧糖的一个错误。因为我们还看到同样的错误仅发生在该特定设备 (Galaxy J7 2016) 和特定(旧)版本的 Android (7.1.1) 上。没有其他 Android 版本导致此问题。因此,我们可以安全地忽略此错误,因为代码不指向任何特定行,并且发生率微不足道。