0

我试图在异步 Web 服务调用后显示列表视图,但由于某种原因,此代码失败....有什么建议吗?现在,它只是使应用程序崩溃。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Test.MyWeb;

namespace Test
{
    [Activity(Label = "Favorites")]
    public class FavoritesActivity : ListActivity
    {
        private ProgressDialog d;
        private TextView tv;
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            d = new ProgressDialog(this);
            d.SetTitle("Loading...");
            d.SetMessage("Please wait...");
            d.SetIcon(Resource.Drawable.chef_icon);
            d.Show();



            Test.MyWeb.Daedalus Service = new Daedalus();

            Service.getCategoriesCompleted += Service_OnComplete;
            Service.getCategories();


        }

        private void Service_OnComplete(object s, getCategoriesCompletedEventArgs e)
        {

            try
            {
                d.Hide();

                List<Test.MyWeb.CATEGORY> Categories = e.Result.ToList();
                List<string> cats = new List<string>();

                foreach (Test.MyWeb.CATEGORY Item in Categories)
                {
                    cats.Add(Item.NAME);

                }

                ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.Favorites, cats);

                ListView.TextFilterEnabled = true;

                ListView.ItemClick += delegate(object sender, ItemEventArgs args)
                {
                    // When clicked, show a toast with the TextView text
                    Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).
                        Show();
                };



            } catch(Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Short).Show();

            }
        }

    }
}
4

1 回答 1

4

如果真的是异步调用,则需要在UI线程上修改UI,而不是后台线程:

http://mono-android.net/Documentation/Guides/Writing_Responsive_Applications

无论如何,您应该能够在 Android 日志中找到错误:

http://mono-android.net/Documentation/Guides/Android_Debug_Log

于 2011-04-24T06:12:33.880 回答