我有我的活动,我用它来下载他们的LoveFilm队列中的用户片以下,但ProgressDialog永远不会出现。
I have the following in my Activity that I use to download a users films in their LoveFilm queue, but the ProgressDialog never appears.
public class MyListActivity extends Activity {
SharedPreferences prefs;
ProgressDialog m_progressDialog;
Thread listThread;
User user;
private Runnable threadProc_initializeQueue = new Runnable() {
public void run() {
user.fetchQueues();
Queue defaultQueue = user.getDefaultQueue();
defaultQueue.fetchTitles();
m_progressDialog.dismiss();
}
};
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
// Authenticate the user if needs be.
if(!prefs.getBoolean("isAuthenticated", false)) {
Intent i = new Intent(this, OAuthActivity.class);
startActivity(i);
finish();
} else {
// Get the users default list.
LoveDroid app = (LoveDroid) getApplication();
user = new User(app);
m_progressDialog = ProgressDialog.show(MyListActivity.this, "Please Wait", "Loading", true);
listThread = new Thread(null, threadProc_initializeQueue);
listThread.run();
}
}
我见过别人这个问题,他们都基本上得到各地推荐的线路,看起来像我的
I've seen others with this problem, and they all basically get around to recommending a line that looks like mine
m_progressDialog = ProgressDialog.show(MyListActivity.this, "Please Wait", "Loading", true);
在code ++工程的其余部分,用户薄膜通过线程下载,但该对话框一直没有出现,它需要几秒钟也一样,它不是像对话被解雇之前,它是有时间出现
The rest of the code works, the users films are downloaded via the thread, but the dialog never shows up, it takes a few seconds too, it's not like the dialog is being dismissed before it's had time to appear.
推荐答案
这是更好,如果你使用的AsyncTask 。(而不是线程 - 这是Android的活动一般好的做法)
It's better if you use AsyncTask (instead of thread - it is generally good practice in Android activities).
创建的AsyncTask类,并在该类中添加进度上preExecute对话框,并在onPostExecute驳回。 你可以在这里找到一个例子。
Create an AsyncTask class, and on that class add a progress dialog at onPreExecute, and dismiss it at onPostExecute. You can find an example here.
除此之外,还有你的code的几个问题:
Other than that, there are a few problems in your code:
在调用ProgressDialog.show(...)在的onCreate(...)通常是有问题的(因为你不会看到它,直到的onCreate将完成,这是通常的方式后,你的背景活动已经结束了)。 在所有你的进度对话框的操作,应仅在UI线程执行的,所以你不能在一些随机的线程中使用m_pd.dismiss()(这就是为什么你应该使用的相关推荐
最新文章