我得到以下異常:Xamarin的Android通知崩潰
Java.Lang.IllegalStateException:指定的孩子已經有一個父。您必須在孩子的父母的拳頭上調用removeVeiw()。
運行以下代碼塊時。這一切都始於我的「表單」頁面,並使用按鈕按鈕使用警報管理器生成預定通知。問題是,如果點擊通知,如果應用程序仍處於活動狀態,則會引發上述異常。
如果我在手機上切換應用程序,然後單擊通知,它會使應用程序按預期恢復,而不會發生崩潰。
下面是有問題的代碼段用於此工作流程:
從xamarin頁:
private void BtnStartJob_Clicked(object sender, EventArgs e)
{
lblStatus.Text = "";
if (btnStartJob.Text == "Start Job")
{
tmrToggle.StartCommand.Execute(null);
App.Manager.StartJob(App.Manager.currentTimesheet.ProjectID);
btnStartJob.Text = "Start Break";
}
else if (btnStartJob.Text == "End Break")
{
tmrAlert.PauseCommand.Execute(null);
tmrToggle.StartCommand.Execute(null);
App.Manager.EndBreak(App.Manager.currentTimesheet.TimesheetID);
btnStartJob.Text = "Start Break";
var notificationService = DependencyService.Get<INotificationService>();
notificationService.CancelNotification();
}
else if (btnStartJob.Text == "Start Break")
{
tmrAlert.StartCommand.Execute(null);
tmrToggle.PauseCommand.Execute(null);
App.Manager.StartBreak(App.Manager.currentTimesheet.TimesheetID);
btnStartJob.Text = "End Break";
// schedule the notification here.
var notificationService = DependencyService.Get<INotificationService>();
notificationService.CreateNotification("Take Action", "Your break started 1 second ago, please take action.", TimeSpan.FromSeconds(15).Ticks);
}
}
NotificationService
public class NotificationService : INotificationService
{
public void CancelNotification()
{
var alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));
var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
alarmManager.Cancel(pending);
}
public void CreateNotification(string title, string message, long durationInTicks)
{
var duration = TimeSpan.FromTicks(durationInTicks);
var alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra("title", title);
alarmIntent.PutExtra("message", message);
var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
alarmManager.Set(AlarmType.ElapsedRealtime, duration.Milliseconds, pending);
}
}
報警接收機:
[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var pending = PendingIntent.GetActivity(context, 0,
resultIntent,
PendingIntentFlags.CancelCurrent);
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.WESSUClogo)
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(1337, notification);
}
}
MainActivity: 全球:: Xamarin.Forms.Platform.Android.FormsApplicationActivity {
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
如何解決有什麼建議?對我來說,最大的問題是我需要調用removeView()的對象是什麼?這個異常在LoadApplication(newApp())上拋出;在我的MainActivity線。
由於完整的堆棧跟蹤非常長,請點擊here獲取pastebin。
你有可能提供完整的堆棧跟蹤嗎? – pushasha
你沒有你的AlarmReceiver - 你發佈了兩次NotificationService。如果你能告訴我們哪個特定的行導致異常 – Jason
添加堆棧跟蹤 – Nebri