退出应用程序时在 Android 上的 Xamarin.Forms 中获取 NullReferenceException
Posted
技术标签:
【中文标题】退出应用程序时在 Android 上的 Xamarin.Forms 中获取 NullReferenceException【英文标题】:Getting a NullReferenceException in Xamarin.Forms on Android when backing out of app 【发布时间】:2019-09-30 19:09:40 【问题描述】:我正在使用 Xamarin Forms 4.2.0 并使用 Xamarin.Forms Shell。在我的应用程序的各个屏幕中导航时,按下后退按钮似乎可以找到,但是,当我在调试时按下返回退出应用程序时,我得到一个 NullReferenceException:
09-30 14:49:52.866 D/Mono ( 1085): Loading reference 10 of /storage/emulated/0/android/data/com.companyname.agentconnectmobile/files/.__override__/Xamarin.Forms.Platform.Android.dll asmctx DEFAULT, looking for System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.866 D/Mono ( 1085): Assembly Ref addref Xamarin.Forms.Platform.Android[0x799acc0e00] -> System.Runtime.Serialization[0x7935aa8d00]: 3
09-30 14:49:52.866 D/Mono ( 1085): Loading reference 1 of System.Runtime.Serialization.dll asmctx DEFAULT, looking for System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.866 D/Mono ( 1085): Assembly Ref addref System.Runtime.Serialization[0x7935aa8d00] -> System.Xml[0x799be73480]: 5
09-30 14:49:52.918 D/Mono ( 1085): Loading reference 2 of System.Runtime.Serialization.dll asmctx DEFAULT, looking for System.ServiceModel.Internals, Version=0.0.0.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.920 D/Mono ( 1085): Image addref System.ServiceModel.Internals[0x7937f9df00] (asmctx DEFAULT) -> System.ServiceModel.Internals.dll[0x7935889800]: 2
09-30 14:49:52.920 D/Mono ( 1085): Prepared to set up assembly 'System.ServiceModel.Internals' (System.ServiceModel.Internals.dll)
09-30 14:49:52.920 D/Mono ( 1085): Assembly System.ServiceModel.Internals[0x7937f9df00] added to domain RootDomain, ref_count=1
09-30 14:49:52.922 D/Mono ( 1085): AOT: image 'System.ServiceModel.Internals.dll.so' not found: dlopen failed: library "System.ServiceModel.Internals.dll.so" not found
09-30 14:49:52.923 D/Mono ( 1085): AOT: image '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-arm64-v8a-release/lib/mono/aot-cache/arm64/System.ServiceModel.Internals.dll.so' not found: (null)
09-30 14:49:52.923 D/Mono ( 1085): Config attempting to parse: 'System.ServiceModel.Internals.dll.config'.
09-30 14:49:52.923 D/Mono ( 1085): Config attempting to parse: '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-arm64-v8a-release/etc/mono/assemblies/System.ServiceModel.Internals/System.ServiceModel.Internals.config'.
09-30 14:49:52.923 D/Mono ( 1085): Assembly Ref addref System.Runtime.Serialization[0x7935aa8d00] -> System.ServiceModel.Internals[0x7937f9df00]: 2
09-30 14:49:52.923 D/Mono ( 1085): Loading reference 0 of System.ServiceModel.Internals.dll asmctx DEFAULT, looking for mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.923 D/Mono ( 1085): Assembly Ref addref System.ServiceModel.Internals[0x7937f9df00] -> mscorlib[0x7a3039be80]: 64
09-30 14:49:52.923 D/Mono ( 1085): Loading reference 2 of System.ServiceModel.Internals.dll asmctx DEFAULT, looking for System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.924 D/Mono ( 1085): Assembly Ref addref System.ServiceModel.Internals[0x7937f9df00] -> System.Xml[0x799be73480]: 6
Loaded assembly: System.ServiceModel.Internals.dll [External]09-30 14:49:52.924 D/Mono ( 1085): Loading reference 3 of System.Runtime.Serialization.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.924 D/Mono ( 1085): Assembly Ref addref System.Runtime.Serialization[0x7935aa8d00] -> System[0x799acd1580]: 10
09-30 14:49:52.998 D/Mono ( 1085): Loading reference 1 of System.Xml.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:52.999 D/Mono ( 1085): Assembly Ref addref System.Xml[0x799be73480] -> System[0x799acd1580]: 11
09-30 14:49:53.099 D/Mono ( 1085): Loading reference 1 of System.ServiceModel.Internals.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:53.100 D/Mono ( 1085): Assembly Ref addref System.ServiceModel.Internals[0x7937f9df00] -> System[0x799acd1580]: 12
09-30 14:49:53.145 D/Mono ( 1085): Loading reference 19 of /storage/emulated/0/Android/data/com.companyname.agentconnectmobile/files/.__override__/Xamarin.Forms.Platform.Android.dll asmctx DEFAULT, looking for System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=abc123
09-30 14:49:53.146 D/Mono ( 1085): Assembly Ref addref Xamarin.Forms.Platform.Android[0x799acc0e00] -> System.Xml[0x799be73480]: 7
09-30 14:49:53.221 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.221 D/Mono ( 1085): Searching for 'SystemNative_Unlink'.
09-30 14:49:53.221 D/Mono ( 1085): Probing 'SystemNative_Unlink'.
09-30 14:49:53.222 D/Mono ( 1085): Found as 'SystemNative_Unlink'.
09-30 14:49:53.279 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.279 D/Mono ( 1085): Searching for 'SystemNative_LStat2'.
09-30 14:49:53.279 D/Mono ( 1085): Probing 'SystemNative_LStat2'.
09-30 14:49:53.279 D/Mono ( 1085): Found as 'SystemNative_LStat2'.
09-30 14:49:53.279 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.279 D/Mono ( 1085): Searching for 'SystemNative_Rename'.
09-30 14:49:53.279 D/Mono ( 1085): Probing 'SystemNative_Rename'.
09-30 14:49:53.280 D/Mono ( 1085): Found as 'SystemNative_Rename'.
09-30 14:49:53.553 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.553 D/Mono ( 1085): Searching for 'SystemNative_Link'.
09-30 14:49:53.553 D/Mono ( 1085): Probing 'SystemNative_Link'.
09-30 14:49:53.553 D/Mono ( 1085): Found as 'SystemNative_Link'.
09-30 14:49:53.563 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.563 D/Mono ( 1085): Searching for 'SystemNative_Stat2'.
09-30 14:49:53.563 D/Mono ( 1085): Probing 'SystemNative_Stat2'.
09-30 14:49:53.563 D/Mono ( 1085): Found as 'SystemNative_Stat2'.
09-30 14:49:53.564 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.564 D/Mono ( 1085): Searching for 'SystemNative_Symlink'.
09-30 14:49:53.564 D/Mono ( 1085): Probing 'SystemNative_Symlink'.
09-30 14:49:53.564 D/Mono ( 1085): Found as 'SystemNative_Symlink'.
09-30 14:49:53.588 W/com.companyname.agentconnectmobile( 1085): type=1400 audit(0.0:4414): avc: denied link for comm=54687265616420506F6F6C20576F72 name="PropertyStore.forms.tmp" dev="dm-6" ino=77573 scontext=u:r:untrusted_app_27:s0:c6,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c6,c257,c512,c768 tclass=file permissive=0
09-30 14:49:53.597 D/Mono ( 1085): DllImport searching in: 'libmono-native.so' ('./libmono-native.so').
09-30 14:49:53.598 D/Mono ( 1085): Searching for 'SystemNative_CopyFile'.
09-30 14:49:53.598 D/Mono ( 1085): Probing 'SystemNative_CopyFile'.
09-30 14:49:53.598 D/Mono ( 1085): Found as 'SystemNative_CopyFile'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
这是我的第一个 Xamarin 应用程序,所以我不确定我是否应该以某种方式在 Android 上处理退出?我在 ios 上没有看到这个问题。
编辑:此 NRE 似乎特定于 Xamarin.Forms 4.2 问题,如答案所示。
【问题讨论】:
能否请您发布一个基本演示,以便我们可以在我这边进行测试? @JessieZhang - FreakyAli 下面的回答解决了我的问题。 【参考方案1】:发生了类似的崩溃,现在问题似乎是 shell 在 4.2.xxx 及更高版本中将空页面推送到 NavStack 中,这很烦人,但就是这样。我可以通过在我的 Apps Shell 类的 OnBackButtonPressed
中编写以下代码来解决这个问题。
protected override bool OnBackButtonPressed()
if (Application.Current.MainPage.GetType() == typeof(AppShell) && Shell.Current.Navigation.NavigationStack.Where(x => x != null).Any())
return base.OnBackButtonPressed();
else
System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();
return true;
AppShell 是我的自定义 Shell 类。
【讨论】:
【参考方案2】:作为记录,这已在https://github.com/xamarin/Xamarin.Forms/issues/6640 中确定。该问题已在 v4.3.0 中修复,但对于仍在使用 v4.2.0 的用户,提供的解决方法是
使用自定义渲染器绕过崩溃,该渲染器不会使处置中的外壳为空,就像这样。
[assembly: ExportRenderer(typeof(Shell), typeof(ShellRendererCustomDispose))] namespace App.Droid.Renderers public class ShellRendererCustomDispose : ShellRenderer bool _disposed; public ShellRendererCustomDispose(Context context) : base(context) protected override void Dispose(bool disposing) if (_disposed) return; if (disposing) Element.PropertyChanged -= OnElementPropertyChanged; Element.SizeChanged -= (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, "OnElementSizeChanged"); // OnElementSizeChanged is private, so use reflection _disposed = true;
【讨论】:
以上是关于退出应用程序时在 Android 上的 Xamarin.Forms 中获取 NullReferenceException的主要内容,如果未能解决你的问题,请参考以下文章