Issue
The code at the bottom of this post is triggered by the following line of code.
new MasterClickAsyncTask(main).execute(position);
The doInBackground portion of code below calls a method containing a for loop, hence the need for Looper.prepare().
This code runs fine the first few times it is called but then it throws the following error:
10-15 22:50:24.752: ERROR/AndroidRuntime(9258): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
I've tried putting Looper.getMainLooper().quit(); and a few other things like this in various parts of the AsyncTask but this has only made it crash immediately.
Any Help?
Code:
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import android.os.AsyncTask;
import android.os.Looper;
import android.view.View;
public class MasterClickAsyncTask extends AsyncTask<Integer, Void, Void> {
Master selectedMaster;
Main main;
MasterGridView masterGridView;
Integer i;
DiscogProxy discogProxy = new DiscogProxy();
MasterClickAsyncTask(Main main){
this.main = main;
this.masterGridView = main.getMasterGridView();
}
@Override
protected void onPreExecute() {
main.progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Integer... params) {
masterGridView.getSelectedView();
Globals.selectedMaster = (Master)(masterGridView).getItemAtPosition(params[0].intValue());
Globals.selectedMaster.getVersionListView().getVersionList().clear();
Looper.prepare();
try {
Globals.selectedMaster.getVersionListView().populateVersionList();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Looper.getMainLooper().quit();
return null;
}
@Override
protected void onPostExecute(Void result) {
main.populateAlbumVersionsView();
main.progressBar.setVisibility(View.GONE);
}
}
Solution
Looper has nothing to do with for loops. The Looper is the part of the Android system that controls the UI thread. There are several things that wont work without preparing the looper, but in general it is best to avoid Looper.prepare();
unless absolutely necessary. It is far better to use the async doInBackground
to perform data processing or other longer operations, and then update the UI with onPostExecute
and onProgressUpdate
.
In short, unless you are using the UI in some way, you don't need to call the Looper. If you find yourself having to call the Looper, you probably need to restructure how your background threads are working.
Answered By - Ashterothi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.