启动Xamarin前景服务样本崩溃
我正在寻找创建Xamarin前景服务,并认为从 https://github.com/xamarin/monodroid-samples/tree/main/main/applicationfundamentals/servicesampleass/foregroundservedemo 将是一个很好的第一步。 构建和部署到我的三星S10后,单击“开始服务”时,它只是崩溃了。
它失败的行是timestampservice.cs第155行,在代码上:
var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);
null引用异常。堆栈跟踪:
at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I (_JniMarshal_PPLII_I callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1, System.Int32 p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:221 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I(intptr,intptr,intptr,int,int)
我已经尝试了一些事情,包括更新到参考文献的最新版本,但我仍然没有解决此问题。 我尝试从头开始创建新的背景服务,但仍然会遇到相同的错误。
有人有什么想法,我如何解决这个问题?
I'm looking to create a Xamarin foreground service and thought taking the sample from https://github.com/xamarin/monodroid-samples/tree/main/ApplicationFundamentals/ServiceSamples/ForegroundServiceDemo would be a good first step.
After building and deploying to my Samsung S10 it just crashes when clicking 'Start Service'.
The line it fails on is TimeStampService.cs line 155, on the code:
var pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);
With null reference exception. The stack trace:
at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I (_JniMarshal_PPLII_I callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0, System.Int32 p1, System.Int32 p2) [0x00022] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:221 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_I(intptr,intptr,intptr,int,int)
I've tried a few things, including updating to latest versions of references, but still nothing I do fixes this.
I tried creating a new background service from scratch and still get the same error.
Does anyone have any ideas how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在AndroidManifest.xml中添加了前景服务的权限?
例如:
更新
您可以尝试在我的旧答案中使用代码:在关闭应用程序关闭应用程序(使用AlarmManager)时在Android中发射本地通知的麻烦
此外,在MainAttivity中启动它:
Did you add the foreground service permission in the AndroidManifest.xml?
Such as:
Update
You can try to use the code in my old answer:Trouble getting local notifications to fire in Android when the application is closed (using AlarmManager)
In addition, start it in the mainactivity: