【问题标题】:Android.Util.AndroidRuntimeException: <Timeout exceeded getting exception details> occurredAndroid.Util.AndroidRuntimeException: <Timeout exceeded getting exception details> 发生
【发布时间】:2019-03-30 23:52:18
【问题描述】:

我是 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# android visual-studio xamarin


    【解决方案1】:

    你不能从后台线程触摸 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) 移动到OnPostExecute(string result),因为if_connected(); 显示一个无法在后台运行的对话框

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

    【讨论】:

    • 谢谢你它工作得很好但是为什么这表明有些东西必须从 RunInBackground(string[] @params) 中删除它
    • 如何使用 uptask.GetResult();从类 uptask 中检索一些变量,但使用 uptask.GetResult();对话进度没有显示原因?
    • @mohamedsayed 当您在后台时,您无法访问 UI(您无法触摸 UI),也无法向用户显示任何 UI(对话框或任何其他 UI)。
    • 但是如果 ui 是繁重的过程我应该怎么做
    • @mohamedsayed 你所说的“重”是什么意思?在 UI 中,您不能做繁重的事情(例如 File、DB 或 NET I/O),如果您这样做,用户将无法与 UI 交互,并且您的应用程序似乎冻结了,没有人希望 UI 冻结并且不响应
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2016-09-13
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多