【发布时间】:2019-09-20 10:20:51
【问题描述】:
我最近发现,当我以发布模式在 android 版本上运行我的应用程序时,我的“专业”下拉选项字段为空,没有可更改的选项。问题是很难调试问题,因为我无法在问题发生之前单步执行我的代码并分析变量值。我已经搜索了几天以通过写入控制台、制作包含字符串的文件等来查看值的任何方式,但没有运气。
幸运的是,我设法通过使用 alertdisplays 找到问题所在在应用程序崩溃之前声明。如果没有崩溃,则表明 try 语句必须在警报显示之前的行切换到 catch。
需要注意的几点:
- 它只在 android 发布模式下中断
- 在 IOS 发布和调试模式下运行良好
- 如果我启用共享单声道运行时(但这意味着我无法发布应用),则发布模式适用于 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)
【问题讨论】:
-
如果它崩溃了,可能有一个堆栈跟踪和一个异常。请提供。
-
HttpClient 也应该被视为共享资源。所以不要在每次需要取东西时都更新一个。似乎也缺少
.ConfigureAwait(false),因为大部分代码不需要返回上下文 -
提示:考虑将
HttpClient包装在using语句中。 -
Cheesebaron 我已经添加了应用程序崩溃时的控制台输出,但您是否必须处于调试模式才能查看堆栈跟踪?
-
您在 PathwaySearch 中有一个 InvalidCastException
标签: c# android debugging xamarin