我有一个代码在活动中每分钟运行一次,因为我已经将 KeepScreenOn 标记为我,但我可以毫无问题地运行此方法:
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);
}
}
}
但有时我在 results = await query.FindAsync(); 上得到了对象null异常,我无法理解为什么。是因为代码有问题还是因为我应该使用后台服务?
感谢
现在我有这个堆栈跟踪从视觉工作室日志,我今天无法重现问题,当我有一个更好的堆栈跟踪我将再次发布 堆栈跟踪:
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