Android 重复任务在某些重复后引发null异常

Android 重复任务在某些重复后引发null异常,android,xamarin,service,xamarin.android,background-process,Android,Xamarin,Service,Xamarin.android,Background Process,我有一个在活动中每分钟运行一次的代码,因为我将KeepScreenOn标记放在了I上,尽管我可以毫无问题地运行此方法: private async Task UpdateData(CancellationToken token) { Vibrator vibrator = (Vibrator)this.ApplicationContext.GetSystemService(Context.VibratorService); whi

我有一个在活动中每分钟运行一次的代码,因为我将KeepScreenOn标记放在了I上,尽管我可以毫无问题地运行此方法:

 private async Task UpdateData(CancellationToken token)
        {
            Vibrator vibrator = (Vibrator)this.ApplicationContext.GetSystemService(Context.VibratorService);
            while (!token.IsCancellationRequested)
            {
                await Task.Delay(60000);
                // Get ETA
                try
                {
                    query = ParseObject.GetQuery("CaseDetails");
                    results = await query.FindAsync();
                    resultsList = results.ToList();
                    foreach (var temp in resultsList)
                    {
                        // If ETA is changed, Update ETA and vibrate
                        if (Convert.ToDateTime(application.caseObject["ETA"]) != temp.Get<DateTime>("ETA"))
                        {
                            ctsETA.Cancel();
                            ctsETA = new CancellationTokenSource();
                            _tokenETA = ctsETA.Token;
                            application.caseObject = temp;
                            dateDiff = Convert.ToDateTime(application.caseObject["ETA"]) - DateTime.Now;
                            StartCountdownTimer(ctsETA.Token, dateDiff);
                            vibrator.Vibrate(500);
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.Write("Exception occured: " + ex);
                }
            }
        }
专用异步任务更新数据(CancellationToken令牌)
{
可控震源=(可控震源)this.ApplicationContext.GetSystemService(Context.可控震源服务);
而(!token.IsCancellationRequested)
{
等待任务。延迟(60000);
//得到预计到达时间
尝试
{
query=ParseObject.GetQuery(“CaseDetails”);
结果=wait query.FindAsync();
resultsList=results.ToList();
foreach(结果列表中的变量临时值)
{
//如果ETA发生变化,则更新ETA并振动
if(Convert.ToDateTime(application.caseObject[“ETA”])!=temp.Get(“ETA”))
{
ctsETA.Cancel();
ctsETA=新的CancellationTokenSource();
_tokenETA=ctsETA.Token;
application.caseObject=temp;
dateDiff=Convert.ToDateTime(application.caseObject[“ETA”])-DateTime.Now;
StartCountdownTimer(ctsETA.Token,dateDiff);
振动器。振动器(500);
}
}
}
捕获(例外情况除外)
{
Console.Write(“发生异常:+ex”);
}
}
}
但有时我会在results=wait query.FindAsync()上得到对象null异常;我不明白为什么。这是因为代码有问题还是因为我应该为此使用后台服务

谢谢

现在我已经把这个stacktrace记录在了frm visual studio日志中,我今天无法重现这个问题,当我有一个更好的stacktrace时,我会再次发布 堆栈跟踪:

Time    Device Name Type    PID Tag Message
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime      ... 1 more
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  Caused by: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at Parse.Internal.HttpClient+<>c__DisplayClass15.<ExecuteAsync>b__d (System.Threading.Tasks.Task`1[TResult] t) [0x00093] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:372)
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  Caused by: java.lang.reflect.InvocationTargetException
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0004e] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0004e] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  Process: com.cdrosos.iSurgery, PID: 11861
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in <3dc9ed3d31194319991e686734adcb10>
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:372)
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime      at java.lang.reflect.Method.invoke(Native Method)
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at Parse.Internal.HttpClient+<>c__DisplayClass15.<ExecuteAsync>b__d (System.Threading.Tasks.Task`1[TResult] t) [0x00093] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime      ... 1 more
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  Caused by: java.lang.reflect.InvocationTargetException
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime  Caused by: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
01-07 20:18:51.894  Samsung GT-I9301I   Error   11861   AndroidRuntime    at System.Threading.Tasks.Task.Execute () [0x00016] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  FATAL EXCEPTION: main
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  --- End of stack trace from previous location where exception was thrown ---
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime    at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00027] in <3dc9ed3d31194319991e686734adcb10>:0 
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime      at java.lang.reflect.Method.invoke(Native Method)
01-08 04:24:26.846  Samsung GT-I9301I   Error   1785    AndroidRuntime  java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
时间设备名称类型PID标记消息
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]in:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime--来自引发异常的前一个位置的堆栈结束跟踪---
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]in:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于System.Threading.Tasks.Task.Execute()[0x00016]中:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime。。。还有一个
01-08 04:24:26.846 Samsung GT-I9301I错误1785 AndroidRuntime导致的错误:android.runtime.JavaProxyThrowable:System.NullReferenceException:对象引用未设置为对象的实例
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于Parse.Internal.HttpClient+c__DisplayClass15.b_u d(System.Threading.Tasks.Task`1[TResult]t)[0x00093]in:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke()[0x00027]中:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke()[0x00027]中:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime--来自引发异常的前一个位置的堆栈结束跟踪---
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于System.Threading.Tasks.Task.Execute()[0x00016]中:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于java.lang.reflect.Method.invoke(Method.java:372)
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于Parse.Internal.InternalExtensions+c_uDisplayClass7`1[TResult].b_u6(System.Threading.Tasks.Task t)[0x00033]in:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime由以下原因引起:java.lang.reflect.InvocationTargetException
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime at System.Runtime.compilers.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0004e]in:0
01-08 04:24:26.846三星GT-I9301I错误1785 AndroidRuntime位于System.Threading.Tasks.Task.Execute()[0x00016]中:0
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime位于System.Threading.Tasks.Task.Execute()[0x00016]中:0
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime--来自引发异常的前一个位置的堆栈结束跟踪---
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]中:0
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime--来自引发异常的前一个位置的堆栈结束跟踪---
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime位于System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke()[0x00027]中:0
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0004e]中:0
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime进程:com.cdrosos.iSurgery,PID:11861
01-07 20:18:51.894三星GT-I9301I错误11861 AndroidRuntime位于Parse.Internal.InternalExtensions+c__DisplayClass7`