我開發的應用程序中加載到ArrayList的數據需要2/3秒。我想要顯示將數據加載到ArrayList的進度。顯示進度對話框,直到將數據加載到列表
我正在使用AsyncTask。我通過單擊按鈕執行此操作,當ArrayList完全加載時,它將通過Intent傳遞給下一個活動。是
showList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (session.isOpened()) {
if (friendID.size() == 0 && friendName.size() == 0) {
new Async().execute();
}
}
});
Ansyc Task類如下 - - 巴頓
代碼片段如下 -
的onClick
class Async extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setTitle("Loading...");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setMessage("Please wait for completion");
dialog.setProgress(0);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
Request.executeMyFriendsRequestAsync(session,
new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users,
Response response) {
Iterator<GraphUser> iterator = users.iterator();
GraphUser graphUser = null;
friendID.clear();
friendName.clear();
while (iterator.hasNext()) {
if (iterator != null) {
graphUser = iterator.next();
Log.d("Friend Information ",
" friend ID " + graphUser.getId()
+ " friend Name "
+ graphUser.getName());
friendID.add(graphUser.getId());
friendName.add(graphUser.getName());
publishProgress(friendID.size());
Log.d("Friend Information ", " friendID= "
+ friendID.size() + " friendName "
+ friendName.size());
}
}
}
});
dialog.dismiss();
return "sucess";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Intent intent = new Intent(MainActivity.this, FriendsList.class);
Log.d("Checking the size ", "ID " + friendID.size() + " UserName "
+ friendName.size());
/*
* Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT)
* .show();
*/
intent.putStringArrayListExtra("userID", friendID);
intent.putStringArrayListExtra("userName", friendName);
intent.putExtra("str", result);
startActivity(intent);
}
@Override
protected void onProgressUpdate(Integer... values) {
dialog.setProgress(values[0]);
//dialog.incrementProgressBy(values[0]);
super.onProgressUpdate(values);
}
}
在登錄貓的錯誤 -
04-12 14:15:56.595: E/AndroidRuntime(15794): FATAL EXCEPTION: AsyncTask #2
04-12 14:15:56.595: E/AndroidRuntime(15794): **java.lang.RuntimeException: An error occured while executing doInBackground()**
04-12 14:15:56.595: E/AndroidRuntime(15794): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-12 14:15:56.595: E/AndroidRuntime(15794): at java.lang.Thread.run(Thread.java:1019)
04-12 14:15:56.595: E/AndroidRuntime(15794): **Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()**
嘿感謝你的朋友,我得到了解決。 **由後臺線程執行的oncomplete()**方法:不要使用此方法更新UI .http://stackoverflow.com/a/8213857/2123594 –