每个线程只能创建一个 Looper 错误、异步任务
本文底部的代码是由以下代码行触发的。
new MasterClickAsyncTask(main).execute(position);
下面代码的 doInBackground 部分调用一个包含 for 循环的方法,因此需要 Looper.prepare()。
此代码在前几次调用时运行良好,但随后抛出以下错误:
10-15 22:50:24.752: ERROR/AndroidRuntime(9258): Caused by: java.lang.RuntimeException: Only one Looper may be create每个线程
我都尝试放置 Looper.getMainLooper().quit();以及 AsyncTask 的各个部分中的其他一些类似的事情,但这只会使其立即崩溃。
有帮助吗?
代码:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Looper 与 for 循环无关。 Looper是Android系统中控制UI线程的部分。如果不准备循环器,有几件事将无法工作,但一般来说,除非绝对必要,否则最好避免
Looper.prepare();
。最好使用异步doInBackground
执行数据处理或其他较长的操作,然后使用onPostExecute
和onProgressUpdate
更新 UI。简而言之,除非您以某种方式使用UI,否则不需要调用Looper。如果您发现自己必须调用 Looper,您可能需要重新构建后台线程的工作方式。
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 asyncdoInBackground
to perform data processing or other longer operations, and then update the UI withonPostExecute
andonProgressUpdate
.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.