发生了,c#,android,visual-studio,xamarin"/>

首页 > 解决方案 > Android.Util.AndroidRuntimeException:发生了

问题描述

我是 android c# 开发人员初学者,需要在后台显示我的列表视图,但我的代码有一个加载操作,例如获取数据库并将字节数组转换为图像,所以我使用了异步任务,但它挂起并给我这个错误未处理的异常:

Android.Util.AndroidRuntimeException:发生

//this my async class :

public class UpdatePB : AsyncTask<string, string, string>
{
    private Activity context;
    private ProgressDialog progressDialog;

    public List<Advertise_TableItem> GroupLst;
    Dictionary<Advertise_TableItem, List<Replies_TableItem>> ChildLst;
    //  Advertise_HomeScreenAdapter adb;
    public ExpandableListViewAdapter Adapter;
    AbdoService.abdo_service AbdoService;

    //string user_id;
    Context myContext;
    ExpandableListView listview;
    DataTable table;
    public UpdatePB(Activity context, ExpandableListView listview, Context myContext)
    {
        this.context = context;
        this.listview = listview;
        this.myContext = myContext;


    }
    protected override string RunInBackground(string[] @params)
    {
        //System.Threading.Thread.Sleep(3000);

        //adb = new Advertise_HomeScreenAdapter(this, tableItems);
        //listview.Adapter = adb;

        //progressDialog = new ProgressDialog(context);
        //progressDialog.Show();


        AbdoService = new AbdoService.abdo_service();

        GroupLst = new List<Advertise_TableItem>();
        ChildLst = new Dictionary<Advertise_TableItem, List<Replies_TableItem>>();
        if (CrossConnectivity.Current.IsConnected)
        {


            table = AbdoService.selectAllAdvertises().table;

            int I = 0;
            foreach (DataRow item in table.Rows)
            {
                // bitmap = BitmapFactory.DecodeByteArray(AbdoService.GetByteImage(item[1].ToString()).ByteImage, 0, AbdoService.GetByteImage(item[1].ToString()).ByteImage.Length);


                GroupLst.Add(new Advertise_TableItem(item[1].ToString(), item[2].ToString(), item[3].ToString(), item[0].ToString()));
                List<Replies_TableItem> child = new List<Replies_TableItem>();
                //child.Add(new Replies_TableItem("mohammed", "comment 1 "));
                //child.Add(new Replies_TableItem("ahmed", "comment 2 "));
                var childTable = AbdoService.selectReply(item[0].ToString());
                foreach (DataRow childItem in childTable.table.Rows)
                {
                    child.Add(new Replies_TableItem(childItem[1].ToString(), childItem[2].ToString()));
                }
                ChildLst.Add(GroupLst[I], child);

                I++;

            }

        }
        else
        {
            if_connected();
        }



        Adapter = new ExpandableListViewAdapter(myContext, GroupLst, ChildLst);
        ////System.Threading.Thread.Sleep(3000);
        listview.SetAdapter(Adapter);


        for (int i = 0; i < Adapter.GroupCount; i++)
        {
            listview.ExpandGroup(i);
        }

        //listview.GroupClick += Listview_GroupClick;

        //startServices();
        ////RunOnUiThread(() => progressBar.Visibility = ViewStates.Invisible);

        return context.Title;

    }
    public void if_connected()
    {





        var callDialog = new Android.App.AlertDialog.Builder(context);
        callDialog.SetTitle("Notify");
        callDialog.SetMessage("connect internet");
        callDialog.SetNeutralButton("ok", delegate
        {
            context.Finish();
            context.StartActivity(typeof(AllAdvertises));
        });
        callDialog.Show();

    }
    protected override void OnPreExecute()
    {
        progressDialog = new ProgressDialog(context);
        progressDialog.Show();

    }

    //protected override void OnProgressUpdate(params string[] values)
    //{
    //    //mtv.Text = Convert.ToString(values[0]);
    //    //Android.Util.Log.Error("lv==", values[0] + "");
    //    listview.SetAdapter(Adapter);


    //    for (int i = 0; i < Adapter.GroupCount; i++)
    //    {
    //        listview.ExpandGroup(i);
    //    }

    //}
    protected override void OnPostExecute(string result)
    {
        result = context.Title;

        progressDialog.Dismiss();
    }


}
//this is my Activity function oncreate()
protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetTheme(Resource.Style.AppTheme);
        SetContentView(Resource.Layout.AllAdvertises);

        //progressDialog = new ProgressDialog(this);
        //progressDialog.SetMessage("hiiiiiiiiiiiii");
        //progressDialog.Show();
        //    System.Threading.Thread.Sleep(1000);



        //ProgressBar progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);

        //RunOnUiThread(() => progressBar.Visibility=ViewStates.Visible);
        //DoSomeWork1(arguments);




        prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        TextView UserName = FindViewById<TextView>(Resource.Id.txtUserName);
        string user_id = prefs.GetString("UserId", "0");
        if (user_id != "0")
        {
            UserName.Text = " Hello " + prefs.GetString("UserName", "0");
        }


        //AbdoService = new AbdoService.abdo_service();

        ////table= b.GetResult();
        ////    //AbdoService.selectAllAdvertisesCompleted += AbdoService_selectAllAdvertisesCompleted;
        ////    //AbdoService.selectAllAdvertisesAsync();
        listview = FindViewById<ExpandableListView>(Resource.Id.EdListView);
        //GroupLst = new List<Advertise_TableItem>();
        //ChildLst = new Dictionary<Advertise_TableItem, List<Replies_TableItem>>();
        //if (CrossConnectivity.Current.IsConnected)
        //{


        //    //table = AbdoService.selectAllAdvertises().table;

        //    int I = 0;
        //    foreach (DataRow item in table.Rows)
        //    {
        //        // bitmap = BitmapFactory.DecodeByteArray(AbdoService.GetByteImage(item[1].ToString()).ByteImage, 0, AbdoService.GetByteImage(item[1].ToString()).ByteImage.Length);


        //        GroupLst.Add(new Advertise_TableItem(item[1].ToString(), item[2].ToString(), item[3].ToString(), item[0].ToString()));
        //        List<Replies_TableItem> child = new List<Replies_TableItem>();
        //        //child.Add(new Replies_TableItem("mohammed", "comment 1 "));
        //        //child.Add(new Replies_TableItem("ahmed", "comment 2 "));
        //        var childTable = AbdoService.selectReply(item[0].ToString());
        //        foreach (DataRow childItem in childTable.table.Rows)
        //        {
        //            child.Add(new Replies_TableItem(childItem[1].ToString(), childItem[2].ToString()));
        //        }
        //        ChildLst.Add(GroupLst[I], child);

        //        I++;

        //    }
        //}
        //else
        //{
        //    if_connected();
        //}
        ////adb = new Advertise_HomeScreenAdapter(this, tableItems);
        ////listview.Adapter = adb;






        //Adapter = new ExpandableListViewAdapter(this, GroupLst, ChildLst);
        UpdatePB uptask = new UpdatePB(this, listview, this);
        uptask.Execute("paramter");
        //uptask.GetResult();
        //listview.SetAdapter(Adapter);
        //for (int i = 0; i < Adapter.GroupCount; i++)
        //{
        //    listview.ExpandGroup(i);
        //}

        listview.GroupClick += Listview_GroupClick;

        startServices();

        //////RunOnUiThread(() => progressBar.Visibility = ViewStates.Invisible);

    }

标签: c#androidvisual-studioxamarin

解决方案


您无法从后台线程触摸 UI,因此您需要移动此行:

listview.SetAdapter(Adapter);
for (int i = 0; i < Adapter.GroupCount; i++)
 {
      listview.ExpandGroup(i);
 }

RunInBackground(string[] @params)OnPostExecute(string result)

if_connected();RunInBackground(string[] @params)to移动,OnPostExecute(string result)因为if_connected();显示了一个不能在后台运行的对话框

并将您的完整堆栈跟踪错误添加到您的问题中,因为除了提到的错误之外,可能还有其他一些错误。


推荐阅读