我开发应用程序,其中加载到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类如下 -
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);
}
}
Log Cat中的错误 -
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()**
答案 0 :(得分:1)
2分:
您的Request.executeMyFriendsRequestAsync
似乎是异步操作,因此您只需请求某些操作并快速从AsyncTask.doInBackground()
返回。因此,使用这样的设计,数据加载本身会在AsyncTask.doInBackground()
已经过去时发生。您需要打开sycn API,以便在AsyncTask.doInBackground()
内加载所有朋友数据。
在dialog.dismiss();
AsyncTask.onPostExecute()
醇>