在发布模式下,Xamarin Android 应用闪退(Parse.Android SDK)

6

我正在开发一个使用Parse Android SDK的应用程序。在调试模式下,应用程序可以正常工作,但是当我在发布模式下进行编译时,会出现以下错误。错误发生在执行Parse查询时,而不是在初始化Parse时。

[MonoDroid] UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid]   at Parse.PlatformHooks+<RequestAsync>d__19.MoveNext () [0x00000] in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[MonoDroid]   at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.TaskActionInvoker+FuncTaskInvoke`1[System.Threading.Tasks.Task`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]]].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[MonoDroid]   at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[System.Collections.Generic.IEnumerable`1[Parse.ParseObject]].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.TaskActionInvoker+FuncTaskInvoke`1[System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[Parse.ParseObject]]].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter`1[System.Collections.Generic.IEnumerable`1[Parse.ParseObject]].GetResult () [0x00000] in <filename unknown>:0 
[MonoDroid]   at ParseDAL.ParseCaller+<Init>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
[mono] 
[mono] Unhandled Exception:
[mono] System.NullReferenceException: Object reference not set to an instance of an object
[mono]   at Parse.PlatformHooks+<RequestAsync>d__19.MoveNext () [0x00000] in <filename unknown>:0 
[mono] --- End of stack trace from previous location where exception was thrown ---
[mono]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[mono]   at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00000] in <filename unknown>:0 
[mono]   at System.Threading.Tasks.TaskActionInvoker+FuncTaskInvoke`1[System.Threading.Tasks.Task`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]]].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
[mono]   at System.Threading.Tasks.Task.InnerInvoke 
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
[mono-rt]   at Parse.PlatformHooks+<RequestAsync>d__19.MoveNext () [0x00000] in <filename unknown>:0 
[mono-rt] --- End of stack trace from previous location where exception was thrown ---
[mono-rt]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
[mono-rt]   at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00000] in <filename unknown>:0 
[mono-rt]   at System.Threading.Tasks.TaskActionInvoker+FuncTaskInvoke`1[System.Threading.Tasks.Task`1[System.Tuple`2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary`2[System.String,System.Object]]]].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
[mono-rt]   at System.Threading.Tasks.Task

以下是代码:

ParseClient.Initialize("App Id", "Dot Net Key");
var userQuery = ParseObject.GetQuery ("Table Name");
var userData = await userQuery.FindAsync ();
foreach (var ud in userData) {
    Console.WriteLine ("UD " + ud.Get<string>(Constants.COL_USER_NAME));
}

我尝试了所有的链接选项,包括“不链接”,“链接所有程序集”和“链接SDK程序集”。但应用程序仍然崩溃。

当我禁用优化时,没有任何变化,感谢提示。 - Swift Sharp
1
@SeeSharp 当我以“调试”模式编译后端代码DLL并在前端项目中引用它们时,无论是在发布模式还是调试模式下都可以正常工作。 这个异常有时会在我们向服务器发送请求(响应挂起)时发生,而网络断开或服务器无法访问。我不知道如何处理它,或者停止崩溃。 - Basit ZIa
请确保您已在发布模式下添加了“Internet”权限。在调试模式下,Internet权限会自动添加到清单文件中。 - Basit ZIa
@ Basit Zia,你的想法真的很有效,兄弟,谢谢你。 - Swift Sharp
顺便说一句,如果你想把评论作为答案发布。 - Swift Sharp
显示剩余3条评论
1个回答

16

这个异常是由于网络问题而不是调试/发布构建引起的。在调试模式下,INTERNET权限会自动添加到清单文件中,但在发布模式下则不会。因此,请在清单文件中添加此权限。

<uses-permission android:name="android.permission.INTERNET" />

在发布模式中将不会再出现这个异常。

在发送请求之前,我们仍然需要检查网络是否可用以解析数据。


太棒了!你救了我的一天。 - Anees Deen
我也可以。 - Atif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接