异步任务进度对话框不会消失
你好 我正在从用户帐户加载推文以显示在列表视图中。现在我想让用户知道他们在等待时发生了什么。我已经实现了异步任务,但由于某种原因,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在 AsyncTask 类所在的主类中声明
ProgressDialog 对话框
,并仅调用dialog.show
和dialog.dismiss
方法AsyncTask 类。Try declaring the
ProgressDialog dialog
in the main class in which your AsyncTask class exists and only call thedialog.show
anddialog.dismiss
method in the AsyncTask class.在 Activity 中使用
onCreateDialog(int id)
创建ProgressDialog
。在 AsyncTask 调用中:关闭:
对话框与活动的上下文连接。重新创建活动时,也应该重新创建对话框,但对话框的实例不一样。
Use
onCreateDialog(int id)
in activity to createProgressDialog
. In AsyncTask call:To dismiss:
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.
我遇到了同样的问题,是的,当 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...
onPostExecute() 与声明 Void 不匹配。我在路上,所以我突然想到:
更多 这里。
onPostExecute() does not match the declaration Void. I am on the road so off the top of my head consider:
More here.
嗯,这里想到两件事..
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