简体   繁体   English

Xamarin.Android:异步任务引发未处理的异常

[英]Xamarin.Android: Async Task throws unhandled exception

I have a Xamarin Android application where I am getting data from a web service and then binding it to the list view. 我有一个Xamarin Android应用程序,我在其中从Web服务获取数据,然后将其绑定到列表视图。

The method in MainActivity.cs is the following: MainActivity.cs中的方法如下:

private async Task LoadDataFromService()
{

    int userId = int.Parse(applicationData[USER_ID]); // Shared prefrenced data

    //ASYNC Method getting data from JSON and converting it into Sales Object
    Task<IList<Sales>> apiDataTask = UserData.GetSales(userId); 
    IList<Sales> apiData = await apiDataTask;
    if (apiData != null)
    {
        salesList = apiData as List<Sales>; 
        dbManager.Update(salesList); //Sync with SQLite DB

    }
}

In my MainActivity I have defined a global variable which gives this task an identifier 在我的MainActivity我定义了一个全局变量,该变量为该任务提供了一个标识符

public class MainActivity : Activity
{
    private Task loadData;
}

And I am calling this as follows: 我这样称呼它:

public class MainActivity : Activity
{
    private Task loadData;
    protected override void OnCreate(Bundle bundle)
    { 
        loadData = new Task(async () => { await LoadDataFromService(); });
    }
}

Then I have to wait till the data is downloaded and then I put the data in ListView . 然后,我必须等到数据下载完毕后,再将数据放入ListView

protected override void OnStart()
{
     base.OnStart();
     loadData.Start(); // This is causing to generate an exception
     loadData.ContinueWith(t => { BindData(); }).Wait();
 }

The only issue I am getting is that it ends in an unhanled exception the message saying An unhandled exception occured. 我得到的唯一问题是,它以An unhandled exception occured.结尾,并显示一条消息,指出An unhandled exception occured. , the message comes after OnStart has fired and there is no indication as to what the issue may be. ,该消息是在OnStart触发后发出的,并且没有迹象表明可能是问题所在。

I'm not sure what causes your Exception, because this Message is very poor. 我不确定是什么原因导致您的异常,因为此消息非常糟糕。 But I found an issue which you should fix and which maybe is enough to make your code running. 但是我发现了一个应该修复的问题,也许足以使您的代码运行。 In the OnStart() Method you are using .Wait(); OnStart()方法中,您正在使用.Wait(); . This can cause a deadlock and will block your UI. 这可能会导致死锁,并会阻塞您的UI。 Read more about here . 在此处了解更多信息。 Further I think you don't need to store your Task. 此外,我认为您不需要存储您的任务。 You can await it in OnStart() . 您可以在OnStart()等待它。

So I would recommend you following OnStart(); 因此,我建议您遵循OnStart(); :

protected override async void OnStart()
{
     base.OnStart();

     var data = await LoadDataFromService();
     BindingData();    
 }

Further it's convention that all async methods are named with Async as ending. 此外,约定所有异步方法都以Async作为结尾命名。 Maybe this can help you. 也许这可以帮助您。 If this doesn't solve your issue please try to figure out the inner-Exception. 如果这不能解决您的问题,请尝试找出inner-Exception。 Just debug over your Exception and choose the Property "InnerException" to see it. 只需调试您的异常,然后选择属性“ InnerException”即可查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我在清单文件中添加其接收者时,Xamarin.Android NetworkStatusChangedReceiver都会提供未处理的异常 - Xamarin.Android NetworkStatusChangedReceiver gives unhandled exception whenever i add its receiver in manifest file 以异步方式刷新 Xamarin.Android 中的 AudioTrack - Flush AudioTrack in Xamarin.Android in an async way Xamarin.Android中的重复后台任务 - Recurring background task in Xamarin.Android Xamarin.Android 中的文件提供程序异常 - File provider exception in Xamarin.Android 引用Xamarin.Android项目的Xamarin UI测试引发错误 - Xamarin UI Test referencing Xamarin.Android project throws errors Xamarin Android 中的异步任务实现 - Async Task Implementation In Xamarin Android 在模拟器中运行的Xamarin.Android应用中使用Realm会引发RealmFileAccessErrorException - Using Realm in Xamarin.Android app running in emulator throws RealmFileAccessErrorException 使用async / await仍会阻止Xamarin.Android上的UI - Using async/await still blocks UI on Xamarin.Android “ ResolveLibraryProjectImports”任务意外失败。 Mac上的Xamarin.Android - The “ResolveLibraryProjectImports” task failed unexpectedly. Xamarin.Android on Mac xamarin PCL使用WCF:Android中未处理的异常 - xamarin PCL consume WCF: unhandled exception in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM