AsyncTask 在 doInBackground 中崩溃,添加到 listview
当我尝试在后台运行函数 doTask() 时,我崩溃了。我正在尝试使用 new Thread(new Runnable() {}) 并且它仅适用于此部分: handlerForBar.post(new Runnable() {public void run() { doTask(); } }) 但是当 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
I get crash when i'm trying to run function doTask() in the background. I was trying with new Thread(new Runnable() {}) and it works only this section:
handlerForBar.post(new Runnable() {public void run() { doTask(); } })
but the progressBar appears when the work of doTask() is finished. So I thought that AsyncTask could work, but it crashes.
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;
}
...
}
Structure of *.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在
doInBackground
中触摸 UI,您必须在onProgressUpdate
或onPostExecute
等方法中更新 UI。请参阅此处You can't touch the UI in
doInBackground
, you have to update the UI in methods likeonProgressUpdate
oronPostExecute
. See here您无法在 doInBackground 方法中操作 ListView。而是在
onPostExecute
中执行此操作。简而言之,所有需要 UI 更新的操作都需要在 UI 线程上完成,在 AsyncTask 中,它在执行后工作提示:在 doInBackground 中的适配器中插入项目,然后将其设置为在 onPostExecute 中列出
you cant manipulate ListView in
doInBackground
method. instead perform this inonPostExecute
. In short, all the operations which require UI updates need to be done on UI thread and in AsyncTask it works in post executeHint: insert items in your adapter in doInBackground and then set it to list in onPostExecute