我正在使用Eclipse + SDK 8编写Android应用程序,在Android 2.2模拟器上运行。
我创建了一个AsyncTask来处理后台冗长的任务。以下是代码:
public class taskSavingFavourite extends AsyncTask<Void, Void, Integer>
{
@Override
protected Integer doInBackground(Void... arg0)
{
List<MainListClass> mList = new ArrayList<MainListClass>();
String s = null;
int addedCount= 0;
if (mainListSubTabSelectedTab == 0) mList = buildingList;
else mList = storeList;
if (mList == null) return 0;
try
{
for (int i = 0; i < mList.size(); i ++)
{
if (mList.get(i).checked == 1)
{
s = htFavourite.get(mList.get(i).id+"-"+mList.get(i).type);
if (s == null)
{
addedCount ++;
if (!favouriteList.equals(""))
favouriteList = favouriteList + ";";
favouriteList = favouriteList +
mList.get(i).id + "-" + mList.get(i).type + ":" +
mList.get(i).name;
htFavourite.put(mList.get(i).id + "-" + mList.get(i).type,
mList.get(i).name);
mList.get(i).favourite = 1;
}
}
publishProgress();
}
if (addedCount != 0)
{
SharedPreferences settings = getSharedPreferences("MGS_PRIVATE_DATA", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("favouriteList", favouriteList);
//Commit the edits!
editor.commit();
}
}
catch (Exception e) {};
return addedCount;
}
}
问题是:当mList很大(1000件以上)时,当前的Activity将被强制关闭并返回启动器Activity。我确信它没有崩溃,因为恼人的“不幸的......”对话框没有出来。
我检查了logcat,当它发生时,它总是有这样的消息:
12-10 14:10:28.772: D/dalvikvm(976): threadid=9: sending two SIGSTKFLTs to threadid=2 (tid=977) to cause debuggerd dump
12-10 14:10:30.842: D/dalvikvm(976): Sent, pausing to let debuggerd run
12-10 14:10:38.903: D/dalvikvm(976): Continuing 12-10 14:10:38.903: E/dalvikvm(976): **VM aborting***
我不确定这是否是android bug或者我错过了什么。
答案 0 :(得分:0)
好吧,我想我找到了原因:永远不要在AsyncTask:DoinBackground
中使用紧密循环。
所以我只是把Thread.Sleep(1)
放在循环中,然后一切都像魅力一样。也许这只会发生在模拟器中,而不是真正的设备(我只是猜测)。