AsyncTask 在 doInBackground 中崩溃,添加到 listview

发布于 2025-01-08 09:59:57 字数 1634 浏览 1 评论 0原文

当我尝试在后台运行函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶花眉 2025-01-15 09:59:57

您无法在 doInBackground 中触摸 UI,您必须在 onProgressUpdateonPostExecute 等方法中更新 UI。请参阅此处

You can't touch the UI in doInBackground, you have to update the UI in methods like onProgressUpdate or onPostExecute. See here

许你一世情深 2025-01-15 09:59:57

您无法在 doInBackground 方法中操作 ListView。而是在 onPostExecute 中执行此操作。简而言之,所有需要 UI 更新的操作都需要在 UI 线程上完成,在 AsyncTask 中,它在执行后工作

提示:在 doInBackground 中的适配器中插入项目,然后将其设置为在 onPostExecute 中列出

you cant manipulate ListView in doInBackground method. instead perform this in onPostExecute. In short, all the operations which require UI updates need to be done on UI thread and in AsyncTask it works in post execute

Hint: insert items in your adapter in doInBackground and then set it to list in onPostExecute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文