当我尝试在后台运行函数 doTask() 时出现崩溃。我正在尝试使用 new Thread(new Runnable() {}) 并且它仅适用于此部分: handlerForBar.post(new Runnable() {public void run() { doTask(); } }) 但是 progressBar 在 doTask() 的工作完成时出现。所以我认为 AsyncTask 可以工作,但它崩溃了。
public void doTask()
{
ListView listView = (ListView)findViewById(R.id.list);
myArray = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hash_map;
hash_map = new HashMap<String, Object>();
hash_map.put(NICK_KEY, "nick");
myArray.add(hash_map);
listView.setAdapter(new myListAdapter(myArray,this));
new myListAdapter(myArray,this).notifyDataSetChanged();
}
private class myThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
doTask(); //try, catch also FC
return null;
}
...
}
*.java的结构:
> public class mainActivity extends Activity{}
> public void onCreate()
> new myThread().execute("");
> public void doTask()
> private class myThread extends AsyncTask<String, Void, String>{}
> protected String doInBackground()
> doTask()
> private class myListView extends BaseAdapter
最佳答案
您不能在doInBackground
中触摸UI,您必须在onProgressUpdate
或onPostExecute
等方法中更新UI。参见 here
关于android - AsyncTask 在 doInBackground 中崩溃,添加到 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9422631/