我的Xamarin.Forms Android应用程序会定期崩溃并出现此错误,但99%的时间AndroidHud (AndHud)组件会按预期呈现其消息对话框。它是从我的MainActivity的UI线程调用的(当时没有其他活动是活动的)。
[mono-rt] Stacktrace:
[mono-rt]
[mono-rt] at <unknown> <0xffffffff>
[mono-rt] at (wrapper managed-to-native) Java.Interop.NativeMethods.java_interop_jnienv_call_void_method_a (intptr,intptr&,intptr,intptr,Java.Interop.JniArgumentValue*) <0x00053>
[mono-rt] at Java.Interop.JniEnvironment/InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference,Java.Interop.JniMethodInfo,Java.Interop.JniArgumentValue*) [0x0006c] in /Users/builder/data/lanes/3511/0e59c362/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11639
[mono-rt] at Java.Interop.JniPeerMembers/JniInstanceMethods.InvokeVirtualVoidMethod (string,Java.Interop.IJavaPeerable,Java.Interop.JniArgumentValue*) [0x00031] in /Users/builder/data/lanes/3511/0e59c362/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:26
[mono-rt] at Android.App.Dialog.SetContentView (Android.Views.View) [0x0002c] in /Users/builder/data/lanes/3511/0e59c362/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Dialog.cs:2160
[mono-rt] at AndroidHUD.AndHUD/<SetupDialog>c__AnonStorey3.<>m__0 (object) <IL 0x000be, 0x00483>
[mono-rt] at Android.App.SyncContext.Send (System.Threading.SendOrPostCallback,object) [0x00025] in /Users/builder/data/lanes/3511/0e59c362/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:26
[mono-rt] at AndroidHUD.AndHUD.SetupDialog (Android.Content.Context,AndroidHUD.MaskType,System.Action,System.Func`4<Android.Content.Context, Android.App.Dialog, AndroidHUD.MaskType, Android.Views.View>) <IL 0x0003d, 0x0023f>
[mono-rt] at AndroidHUD.AndHUD.showStatus (Android.Content.Context,bool,string,AndroidHUD.MaskType,System.Nullable`1<System.TimeSpan>,System.Action,bool,System.Action) <IL 0x000cd, 0x0064f>
[mono-rt] at AndroidHUD.AndHUD.Show (Android.Content.Context,string,int,AndroidHUD.MaskType,System.Nullable`1<System.TimeSpan>,System.Action,bool,System.Action) <IL 0x0002c, 0x0021f>
[mono-rt] at Apollo1.Droid.MainActivity.showProgress (Apollo1.Domain.ProgressEvent) [0x00059] in /Volumes/Devology/Source Code/Agile Orange Ltd/Apollo1.Mobile/Apollo1.Android/MainActivity.cs:254
[mono-rt] at Apollo1.Droid.MainActivity/<handleProgressEvent>c__AnonStorey0.<>m__0 () [0x000c2] in /Volumes/Devology/Source Code/Agile Orange Ltd/Apollo1.Mobile/Apollo1.Android/MainActivity.cs:241
[mono-rt] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/3511/0e59c362/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36
[mono-rt] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/3511/0e59c362/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:81
[mono-rt] at (wrapper dynamic-method) object.3f472f08-eb57-4472-a265-297a4af40e0a (intptr,intptr) <IL 0x00011, 0x0003b>
[mono-rt] at (wrapper native-to-managed) object.3f472f08-eb57-4472-a265-297a4af40e0a (intptr,intptr) <IL 0x00021, 0x000ef>
这就是我通常发起对话的方式......
this.Publish<ProgressEvent>(new ProgressEvent("Lets show this message!"));
这就是听取那个事件
void handleProgressEvent (ProgressEvent progressEvent)
{
Device.BeginInvokeOnMainThread (() => {
// Ensure only one thing can enter at a time
lock (this) { // <-- Added recently to try to solve the problem
if (progressEvent.Hide) {
hideProgress ();
} else {
if (currentProgressMessage != null && !progressEvent.Message.Equals (currentProgressMessage)) {
// Don't bother hiding if it's the same message about to be shown
if (!currentProgressMessage.Equals (progressEvent.Message)) {
hideProgress ();
}
}
// Don't bother showing if it's the same message
if (!progressEvent.Message.Equals (currentProgressMessage)) {
showProgress (progressEvent);
}
currentProgressMessage = progressEvent.Message;
}
}
});
}
显示/隐藏对话框的方法非常简单地委托给组件本身,但我一直在添加日志记录和异常处理(没有区别 - 应用程序仍然崩溃)。
void showProgress (ProgressEvent progressEvent)
{
try {
logger.Log (this, LogLevelEnum.INFO, "AndroidHUD invoking show with Forms.Context" + Forms.Context + " progressEvent.Message:" + progressEvent.Message);
AndroidHUD.AndHUD.Shared.Show (Forms.Context, progressEvent.Message, -1, AndroidHUD.MaskType.Black);
showing++;
logger.Log (this, LogLevelEnum.INFO, "AndroidHUD show completed showing:"+showing);
} catch (Exception e) {
logger.Log (this, LogLevelEnum.ERROR, "AndroidHUD show error caught with reason e:" + e.Message);
logger.Log (this, e);
}
}
void hideProgress ()
{
try {
logger.Log (this, LogLevelEnum.INFO, "AndroidHUD invoking hideProgress with Forms.Context" + Forms.Context);
AndroidHUD.AndHUD.Shared.Dismiss (Forms.Context);
showing--;
currentProgressMessage = null;
logger.Log (this, LogLevelEnum.INFO, "AndroidHUD hideProgress completed showing:" + showing);
} catch (Exception e) {
logger.Log (this, LogLevelEnum.ERROR, "AndroidHUD hideProgress error caught with reason e:" + e.Message);
logger.Log (this, e);
}
}
答案 0 :(得分:0)
对于每个人都有好处,我不相信最终是AndHud的错。
我发现在他们当前的线程上下文中触发了一些事件,而不是在UI线程上。接收侦听器正在执行UI操作。
由于我做了这个改变,我几天都没有看到这个错误。
似乎如果你不继续使用UI线程,事情可能会在某个地方完全不同!