我是android c#开发人员初学者,需要在后台工作以显示我的listview,但是我的代码具有加载操作(例如,提取数据库并从字节数组转换为图像),所以我使用了异步任务,但是挂起并出现此错误未处理的异常:
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);
}
答案 0 :(得分:0)
您无法从后台线程触摸用户界面,因此需要移动此行:
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();
显示了一个对话框,该对话框无法在后台运行
并在问题中添加完整的错误堆栈跟踪信息,因为除了提到的错误之外,还有其他错误。