publishProgress 未调用 onProgressUpdate

发布于 2024-12-01 13:28:22 字数 907 浏览 0 评论 0原文

我有一个 AsyncTask,它使用轮询队列来查看新对象是否已到达。当它检测到新对象时,我让它以字符串形式收集信息,然后发布Progress(info)。在 onProgressUpdate 中,它将字符串添加到列表中。我遇到的问题是程序永远不会进入onProgressUpdate。我在调试器中单步执行它,我看到它调用了publishProgress,但是它从未进入onProgress更新。看起来像这样:

    @Override
    protected void onProgressUpdate(String... values) {
        messageQueue.add(displayName + " : " + values[0]);   //I have a breakpoint here that the program never hits.
        ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
        super.onProgressUpdate(values);
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (true) {
            Packet p = pc.pollResult();
            if(p != null){
                String body = ((Message) p).getBody();   //I have a breakpoint here that the program hits only when a new object is in the queue
                publishProgress(body);
            }
        }

    }

I have an AsyncTask that uses polls a queue to see if a new object has arrived. When it detects the new object, I have it collect information as a string and then publishProgress(info). In onProgressUpdate it adds the string to a list. The problem that I am encountering is that the program never enters onProgressUpdate. I stepped through it in the debugger and I see it call publishProgress, however it never enters onProgress update. looks something like this:

    @Override
    protected void onProgressUpdate(String... values) {
        messageQueue.add(displayName + " : " + values[0]);   //I have a breakpoint here that the program never hits.
        ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
        super.onProgressUpdate(values);
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (true) {
            Packet p = pc.pollResult();
            if(p != null){
                String body = ((Message) p).getBody();   //I have a breakpoint here that the program hits only when a new object is in the queue
                publishProgress(body);
            }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ˇ宁静的妩媚 2024-12-08 13:28:22

我遇到了一个问题,它没有被调用,但我只是注意到我把它放在了错误的类中。它当然应该位于 AsyncTask 块内,但相反我在该块之外创建了它

I had an issue where it wasnt getting called but I just noticed I put the in the wrong class. It of course it suppose to be inside the AsyncTask block but instaead I had created it outside of that block

咋地 2024-12-08 13:28:22

来自 http://developer.android.com/reference/android/os/AsyncTask。 html

线程规则

为了使此类正常工作,必须遵循一些线程规则:

execute(Params...) must be invoked on the UI thread.

From http://developer.android.com/reference/android/os/AsyncTask.html

Threading rules

There are a few threading rules that must be followed for this class to work properly:

execute(Params...) must be invoked on the UI thread.
北陌 2024-12-08 13:28:22

这可能会对遇到真正错误的人有所帮助

请注意,AsyncTask 中的第二个参数指的是 Progress

AsyncTask<Params, Progress, Result>

确保您的数据类型匹配。

AsyncTask<URL, String, Long>{

    protected Long doInBackground(URL... urls) {
    return long;
    }

    protected void onProgressUpdate(String... values) {}


    protected void onPostExecute(Long result) {}

}

This may help someone with genuine error

Notice the second argument in AsyncTask refers to Progress

AsyncTask<Params, Progress, Result>

Make sure your datatype matches.

AsyncTask<URL, String, Long>{

    protected Long doInBackground(URL... urls) {
    return long;
    }

    protected void onProgressUpdate(String... values) {}


    protected void onPostExecute(Long result) {}

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