Android 每个线程错误只能创建一个循环器,异步任务
本文底部的代码由以下代码行触发Android 每个线程错误只能创建一个循环器,异步任务,android,multithreading,android-asynctask,looper,Android,Multithreading,Android Asynctask,Looper,本文底部的代码由以下代码行触发 new MasterClickAsyncTask(main).execute(position); 下面代码的doInBackground部分调用一个包含for循环的方法,因此需要Looper.prepare() 此代码在最初几次调用时运行良好,但随后抛出以下错误: 10-15 22:50:24.752:错误/AndroidRuntime(9258):原因:java.lang.RuntimeException:每个线程只能创建一个循环器 我已尝试放置Looper
new MasterClickAsyncTask(main).execute(position);
下面代码的doInBackground部分调用一个包含for循环的方法,因此需要Looper.prepare()
此代码在最初几次调用时运行良好,但随后抛出以下错误:
10-15 22:50:24.752:错误/AndroidRuntime(9258):原因:java.lang.RuntimeException:每个线程只能创建一个循环器
我已尝试放置Looper.getMainLooper().quit();在AsyncTask的不同部分还有一些类似的东西,但这只会使它立即崩溃
有什么帮助吗
代码:
import java.io.IOException;
导入java.net.MalformedURLException;
导入org.json.JSONException;
导入android.os.AsyncTask;
导入android.os.Looper;
导入android.view.view;
公共类MasterClickAsyncTask扩展了AsyncTask{
大师选大师;
主要的;
MasterGridView MasterGridView;
整数i;
DiscogProxy DiscogProxy=新DiscogProxy();
主ClickAsyncTask(主){
this.main=main;
this.masterGridView=main.getMasterGridView();
}
@凌驾
受保护的void onPreExecute(){
main.progressBar.setVisibility(View.VISIBLE);
}
@凌驾
受保护的Void doInBackground(整数…参数){
masterGridView.getSelectedView();
Globals.selectedMaster=(Master)(masterGridView.getItemAtPosition(参数[0].intValue());
Globals.selectedMaster.getVersionListView().getVersionList().clear();
Looper.prepare();
试一试{
Globals.selectedMaster.getVersionListView().populateVersionList();
}捕获(格式错误){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//Looper.getMainLooper().quit();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
main.populatealBumVersionView();
main.progressBar.setVisibility(View.GONE);
}
}
活套与for循环无关。Looper是Android系统中控制UI线程的部分。如果不准备looper,有几种方法是行不通的,但通常最好避免looper.prepare()代码>除非绝对必要。最好使用异步doInBackground
执行数据处理或其他更长的操作,然后使用onPostExecute
和onProgressUpdate
更新UI
简而言之,除非您以某种方式使用UI,否则不需要调用Looper。如果您发现自己必须调用Looper,则可能需要重新构造后台线程的工作方式。Looper与for循环无关。Looper是Android系统中控制UI线程的部分。如果不准备looper,有几种方法是行不通的,但通常最好避免looper.prepare()代码>除非绝对必要。最好使用异步doInBackground
执行数据处理或其他更长的操作,然后使用onPostExecute
和onProgressUpdate
更新UI
简而言之,除非您以某种方式使用UI,否则不需要调用Looper。如果您发现自己必须调用循环器,则可能需要重新构造后台线程的工作方式。如果您试图从AsyncTask内部更改UI,请从doInBackground调用publishProgress,然后在onProgressUpdate中进行更改。我能够从doInBackground()中获得所有UI内容方法以最小的麻烦和所有它运行良好。非常感谢你的建议!如果您试图从AsyncTask内部更改UI,请从doInBackground调用publishProgress,然后在onProgressUpdate中进行更改。我能够以最小的麻烦从doInBackground()方法中获得所有UI内容,并且运行良好。非常感谢你的建议!
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);
}
}