gpt4 book ai didi

android - Xamarin 和 Android - 退出方法时崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:19:09 25 4
gpt4 key购买 nike

我不知道还能说些什么。我有这个方法:

public async Task<HttpResponseMessage> SendAsyncRequest(string uri, string content, HttpMethod method, bool tryReauthorizeOn401 = true)
{
HttpRequestMessage rm = new HttpRequestMessage(method, uri);
if (!string.IsNullOrWhiteSpace(content))
rm.Content = new StringContent(content, Encoding.UTF8, "application/json");

HttpResponseMessage response = await client.SendAsync(rm);
if (response.StatusCode == HttpStatusCode.Unauthorized && tryReauthorizeOn401)
{
var res = await AuthenticateUser();
if(res.user == null)
return response;
return await SendAsyncRequest(uri, content, method, false);
}

return response;
}

没什么特别的。client.SendAsync(rm) 已执行,response.StatusCode 正常。应用程序在退出此方法时崩溃。

输出只显示了这个断言:

12-16 20:09:22.025 F/        ( 1683): * Assertion at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/mono/mini/debugger-agent.c:4957, condition `is_ok (error)' not met, function:set_set_notification_for_wait_completion_flag, Could not execute the method because the containing type is not fully instantiated. assembly:<unknown assembly> type:<unknown type> member:(null)
12-16 20:09:22.025 F/libc ( 1683): Fatal signal 6 (SIGABRT), code -6 in tid 1683 (omerang.Android)

仅此而已。客户端是 HttpClient。我在我的 Android 项目中进行了设置:HttpClient Implementation 设置为 Android。

有谁知道哪里出了问题?

编辑

SendAsyncRequest 是这样使用的:

public async Task<(HttpResponseMessage response, IEnumerable<T> items)> GetListFromRequest<T>(string uri)
{
HttpResponseMessage response = await SendAsyncRequest(uri, null, HttpMethod.Get);
if (!response.IsSuccessStatusCode)
return (response, null);

string content = await response.Content.ReadAsStringAsync();
var items = JsonConvert.DeserializeObject<IEnumerable<T>>(content);
return (response, new List<T>(items));
}

最佳答案

基于您提供的示例项目代码

protected override async void OnStart()
{
Controller c = new Controller();
TodoItem item = await c.GetTodoItem(1);
TodoItem item2 = await c.GetTodoItem(2);
}

您正在调用 async void 在启动时触发并忘记。

您将无法捕获任何抛出的异常,这可以解释为什么应用程序会在没有警告的情况下崩溃。

引用 Async/Await - Best Practices in Asynchronous Programming

async void 应该只与事件处理程序一起使用,所以我建议添加一个事件和处理程序。

根据提供的示例,它可能如下所示

public partial class App : Application {
public App() {
InitializeComponent();
MainPage = new MainPage();
}

private even EventHander starting = delegate { };
private async void onStarting(object sender, EventArgs args) {
starting -= onStarting;
try {
var controller = new Controller();
TodoItem item = await controller.GetTodoItem(1);
TodoItem item2 = await controller.GetTodoItem(2);
} catch(Exception ex) {
//...handler error here
}
}

protected override void OnStart() {
starting += onStarting;
starting(this, EventArgs.Empty);
}

//...omitted for brevity
}

这样,您现在至少应该能够捕获抛出的异常以确定失败的原因。

关于android - Xamarin 和 Android - 退出方法时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53805631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com