c# - 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);
}
解决方案
您无法从后台线程触摸 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();
显示了一个不能在后台运行的对话框
并将您的完整堆栈跟踪错误添加到您的问题中,因为除了提到的错误之外,可能还有其他一些错误。
推荐阅读
- python - 使用 matplotlib 和 seaborn 创建圆形密度图
- xml - 如何让 Chrome 加载 XML 原始文件?
- android - 协程组合作业未完成
- docker - 如何从容器中检测主机操作系统?
- c++ - 什么事情会导致 GDB 在点击 main 之前出错?
- python - julia.core.JuliaError:调用 julia 代码时发生异常“ArgumentError”:使用 PyCall
- c++ - 这棵树失去了它的孩子,它只记得最新的。霍夫曼
- vim - VIM 中的颜色高亮函数调用
- c++ - 是否需要将函数声明放在头文件中并将它们的定义放在源文件中?
- java - 致命异常:java.lang.RuntimeException 无法获取应用程序信息