异步任务进度对话框不会消失

发布于 2024-11-09 10:04:49 字数 2782 浏览 1 评论 0原文

你好 我正在从用户帐户加载推文以显示在列表视图中。现在我想让用户知道他们在等待时发生了什么。我已经实现了异步任务,但由于某种原因,onPostExcecute 从未被调用。这就是对话框永远不会被删除的原因。

有人可以给我提示吗?我做错了什么?

如果需要的话,我可以发布我的 TweetAdapterClass

这是我的 AsyncClass

public class ProgressTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        dialog.setTitle("Even geduld...");
        dialog.setMessage("De tweets worden ingeladen...");
        dialog.show();
    }

    protected void onPostExecute() {
        try {
            if (dialog.isShowing()) {
                adaptor = new TweetListAdaptor(MainActivity.this, R.layout.tweetitem, loadTweets());
                setListAdapter(adaptor);
                dialog.dismiss();
            }
        } catch (Exception e) {
        }
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        return null;

    }
}

LoadTweets 看起来像这样:

private ArrayList<Tweets> loadTweets() {
    ArrayList<Tweets> tweets = new ArrayList<Tweets>();
    try {
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new HttpGet(
                "http://search.twitter.com/search.json?q=JobXXL_be&rpp=10");
        // HttpGet get = new
        // HttpGet("http://search.twitter.com/search.json?q=Stijn_messiaen&rp=10");

        HttpResponse rp = hc.execute(get);
        if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(rp.getEntity());
            JSONObject root = new JSONObject(result);
            JSONArray sessions = root.getJSONArray("results");
            for (int i = 0; i < sessions.length(); i++) {
                JSONObject session = sessions.getJSONObject(i);
                Tweets tweet = new Tweets();
                tweet.setTweet(session.getString("text"));
                tweet.setUser(session.getString("from_user"));
                // datum vertalen
                String date = session.getString("created_at").substring(5,
                        16);
                String[] arrDate = date.split(" ");
                int id = this.getResources().getIdentifier(
                        arrDate[1].toString(), "string",
                        this.getPackageName());
                String maand = getResources().getString(id);
                date = arrDate[0].toString() + " " + maand + " "
                        + arrDate[2].toString();

                tweet.setDate(date);
                tweets.add(tweet);
            }
        }
    } catch (Exception e) {
        Log.e("TwitterFeedActivity", "Error loading JSON", e);
    }
    return tweets;
}

编辑:我在我的 doInBackgroundMethod 中添加了我的 LoadTweets() ,这解决了我的问题!

Hello
I'm loading Tweets from a user account to show in a listview. Now I want to let the users know what's going on while they're waiting. I've implementend Async Task, but for some reason, onPostExcecute is never called. That's why the dialog is never removed.

Can someone give me a hint.. What am I doing wrong?

I can post my TweetAdapterClass if that's needed

This is my AsyncClass

public class ProgressTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        dialog.setTitle("Even geduld...");
        dialog.setMessage("De tweets worden ingeladen...");
        dialog.show();
    }

    protected void onPostExecute() {
        try {
            if (dialog.isShowing()) {
                adaptor = new TweetListAdaptor(MainActivity.this, R.layout.tweetitem, loadTweets());
                setListAdapter(adaptor);
                dialog.dismiss();
            }
        } catch (Exception e) {
        }
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        return null;

    }
}

LoadTweets looks like this:

private ArrayList<Tweets> loadTweets() {
    ArrayList<Tweets> tweets = new ArrayList<Tweets>();
    try {
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new HttpGet(
                "http://search.twitter.com/search.json?q=JobXXL_be&rpp=10");
        // HttpGet get = new
        // HttpGet("http://search.twitter.com/search.json?q=Stijn_messiaen&rp=10");

        HttpResponse rp = hc.execute(get);
        if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(rp.getEntity());
            JSONObject root = new JSONObject(result);
            JSONArray sessions = root.getJSONArray("results");
            for (int i = 0; i < sessions.length(); i++) {
                JSONObject session = sessions.getJSONObject(i);
                Tweets tweet = new Tweets();
                tweet.setTweet(session.getString("text"));
                tweet.setUser(session.getString("from_user"));
                // datum vertalen
                String date = session.getString("created_at").substring(5,
                        16);
                String[] arrDate = date.split(" ");
                int id = this.getResources().getIdentifier(
                        arrDate[1].toString(), "string",
                        this.getPackageName());
                String maand = getResources().getString(id);
                date = arrDate[0].toString() + " " + maand + " "
                        + arrDate[2].toString();

                tweet.setDate(date);
                tweets.add(tweet);
            }
        }
    } catch (Exception e) {
        Log.e("TwitterFeedActivity", "Error loading JSON", e);
    }
    return tweets;
}

EDIT: I added my LoadTweets() in my doInBackgroundMethod and that solved my problems!

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

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

发布评论

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

评论(5

感悟人生的甜 2024-11-16 10:04:49

尝试在 AsyncTask 类所在的主类中声明 ProgressDialog 对话框,并仅调用 dialog.showdialog.dismiss 方法AsyncTask 类。

Try declaring the ProgressDialog dialog in the main class in which your AsyncTask class exists and only call the dialog.show and dialog.dismiss method in the AsyncTask class.

简单气质女生网名 2024-11-16 10:04:49

在 Activity 中使用 onCreateDialog(int id) 创建 ProgressDialog。在 AsyncTask 调用中:

MainActivity.this.showDialog(PROGRESS_DIALOG_ID);

关闭:

MainActivity.this.dismissDialog(PROGRESS_DIALOG_ID);

对话框与活动的上下文连接。重新创建活动时,也应该重新创建对话框,但对话框的实例不一样。

Use onCreateDialog(int id) in activity to create ProgressDialog. In AsyncTask call:

MainActivity.this.showDialog(PROGRESS_DIALOG_ID);

To dismiss:

MainActivity.this.dismissDialog(PROGRESS_DIALOG_ID);

Dialogs are connected with activity's context. When activity is recreated dialog should be recreated too but then instance of the dialog is not the same.

画中仙 2024-11-16 10:04:49

我遇到了同样的问题,是的,当 onPostExecute 与声明不匹配时,这发生在我身上...尝试类似 protected Void onPostExecute(Void... arg0)

接下来要做的就是放置 Log .d("您的应用程序","此代码的位置");并检查日志文件中哪些部分不执行...

希望你能找到解决方案...

I had the same problems and yeah this happened to me when onPostExecute didn't match the declaration... try something like protected Void onPostExecute(Void... arg0)

Next thing to do is to put Log.d("Your app", "Location of this code"); and check which part doesn't execute on Log file ...

Hope u will find the solution...

南街九尾狐 2024-11-16 10:04:49

onPostExecute() 与声明 Void 不匹配。我在路上,所以我突然想到:

protected void onPostExecute(Void result)

更多 这里。

onPostExecute() does not match the declaration Void. I am on the road so off the top of my head consider:

protected void onPostExecute(Void result)

More here.

献世佛 2024-11-16 10:04:49

嗯,这里想到两件事..

1)为什么你的 onPostExecute 没有像 onPreExecute 和 doInBackground 那样被重写?

2)为什么doInBackground是在onPost执行之后?一般订单是

onPreExecute
后台操作
执行后

Hmm, two things come to mind here..

1) Why is your onPostExecute not overridden like the onPreExecute and doInBackground?

2) Why is the doInBackground after the onPost Execute? Generally the order is

onPreExecute
doInBackground
onPostExecute

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