Reputation: 2249
I developing the application where data loaded to ArrayList requires to 2/3 seconds. I want to show the progress of loading data to ArrayList.
I am using the AsyncTask for that. I execute this is on click of button and when ArrayList is fully loaded then it will passing to next activity via Intent.
Code Snippet as follows-
onClick of Button -
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 are as follows -
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);
}
}
The error in 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()**
Upvotes: 0
Views: 818
Reputation: 28418
2 points:
Looks like your Request.executeMyFriendsRequestAsync
is an async action so you just request some actions and quickly return from the AsyncTask.doInBackground()
. So, with such a design the data loading itself happens later, when AsyncTask.doInBackground()
has been already passed. You need to switch on a sycn API, so all friends data is loaded while being in AsyncTask.doInBackground()
.
Call dialog.dismiss();
inside of AsyncTask.onPostExecute()
Upvotes: 1