当Xamarin应用仅在发布模式下中断时,我该如何调试?

时间:2019-09-20 10:20:51

标签: c# android debugging xamarin

我最近发现,当我在发布模式下在android版本上运行我的应用程序时,“ Specialty”下拉选项字段为空,没有可供更改的选项。问题是很难调试问题,因为我无法单步执行代码并在问题之前分析变量值。我已经搜寻了几天,寻找通过写入控制台,制作包含字符串但没有运气的文件来查看值的任何方式。

幸运的是,我设法通过使用没有显示但没有使应用崩溃的alertdisplays来定位问题所在,因此我能够看到我可以将alertdisplay放到我的try catch中的高度。声明,它将使应用程序崩溃。如果没有崩溃,则表明try语句必须已切换到警报显示之前的那一行。

一些注意事项:

  • 它仅在android发布模式下中断
  • 在IOS发行和调试模式下正常工作
  • 如果启用共享的单声道运行时,发布模式将在android上运行(但这意味着我无法发布该应用)
  • 调试模式有效,无论是否使用共享单声道运行时
  • 我已经在用于android的物理和虚拟设备上尝试了相同的结果

这是try catch语句,用于从uri中检索一个专业列表(将uri放入我的搜索栏中会向我显示一个正确的专业列表),该捕获了一个例外:

/// Summary: Gets the specialty list 
/// Returns: JArray of specialties   
public async Task<string> getSpecialtyAsync()
{
    HttpClient client = new HttpClient();
    var uri = new Uri(string.Format("https://medphys.royalsurrey.nhs.uk/space/tools/web_services.php?cmd=getSpecialtyList", string.Empty));            
    try
    {
        var response = await client.GetAsync(uri); //This line seems to cause the catch to fire
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }

    }
    catch (Exception ex)
    {
        //How do i print out the exception here, along with some variable values?
    }           
    return null;
}

当我按下带有空白专业字段的搜索按钮时,这会出现在控制台中:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Specified cast is not valid.
[MonoDroid]   at SPACE.PathwaySearch..ctor (System.String searchTerm) [0x00079] in <a380d4923fda450eadc5c42fb0c107d2>:0 
[MonoDroid]   at SPACE.SearchPage+<>c__DisplayClass1_0.<.ctor>b__2 (System.Object s, System.EventArgs e) [0x00167] in <a380d4923fda450eadc5c42fb0c107d2>:0 
[MonoDroid]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <a49690ecb3764be9bb02bbcc2d264f28>:0 
[MonoDroid]   at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[MonoDroid]   at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[MonoDroid]   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[MonoDroid]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.35(intptr,intptr)
[alPhysics.SPAC] JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: com.NHS.rsch.MedicalPhysics.SPACE, PID: 13871
[AndroidRuntime] android.runtime.JavaProxyThrowable: System.InvalidCastException: Specified cast is not valid.
[AndroidRuntime]   at SPACE.PathwaySearch..ctor (System.String searchTerm) [0x00079] in <a380d4923fda450eadc5c42fb0c107d2>:0 
[AndroidRuntime]   at SPACE.SearchPage+<>c__DisplayClass1_0.<.ctor>b__2 (System.Object s, System.EventArgs e) [0x00167] in <a380d4923fda450eadc5c42fb0c107d2>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <a49690ecb3764be9bb02bbcc2d264f28>:0 
[AndroidRuntime]   at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[AndroidRuntime]   at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[AndroidRuntime]   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <6eddb440f9714f0591fc9a4fcb875afa>:0 
[AndroidRuntime]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.35(intptr,intptr)
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.n_run(Native Method)
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
[AndroidRuntime]    at android.os.Handler.handleCallback(Handler.java:873)
[AndroidRuntime]    at android.os.Handler.dispatchMessage(Handler.java:99)
[AndroidRuntime]    at android.os.Looper.loop(Looper.java:193)
[AndroidRuntime]    at android.app.ActivityThread.main(ActivityThread.java:6669)
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
[AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

我从来没有找到过如何调试应用程序的方法,但是我确实找到了解决方法。 我只是去了droid项目->属性-> android清单,并勾选了与此相关的4个框

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