只有一条环线可以每个线程错误,异步任务创建环线、线程、错误、任务

2023-09-06 18:06:49 作者:谁都在装、只是深与浅

在这篇文章底部的code是由下面这行code触发。

 新MasterClickAsyncTask(主).execute(位置); 

低于code的doInBackground部分调用包含一个for循环的方法,因此需要进行活套。prepare()。

这code罚款运行最初几次它叫,但随后引发以下错误:

22 10-15:50:​​24.752:ERROR / AndroidRuntime(9258):了java.lang.RuntimeException:通过只造成一条环线可以每个线程创建

我试过把Looper.getMainLooper()退出()。和其他一些像这样的事情在AsyncTask的各个部分,但这只是使立即崩溃。

任何帮助吗?

code:

 进口java.io.IOException异常;进口java.net.MalformedURLException;进口org.json.JSONException;进口android.os.AsyncTask;进口android.os.Looper;进口android.view.View;公共类MasterClickAsyncTask扩展的AsyncTask<整数,太虚,太虚> {    主selectedMaster;主要为主;MasterGridView masterGridView;整数i;DiscogProxy discogProxy =新DiscogProxy();MasterClickAsyncTask(主主){    this.main =为主;    this.masterGridView = main.getMasterGridView();}@覆盖    在preExecute保护无效(){        main.progressBar.setVisibility(View.VISIBLE);    }       @覆盖        保护无效doInBackground(整数... PARAMS){            masterGridView.getSelectedView();            Globals.selectedMaster =(主)(masterGridView).getItemAtPosition(PARAMS [0] .intValue());            。Globals.selectedMaster.getVersionListView()getVersionList()清()。               尺蠖prepare()。              尝试{                  。Globals.selectedMaster.getVersionListView()populateVersionList();                }赶上(MalformedURLException的E){                    e.printStackTrace();                }赶上(JSONException E){                    e.printStackTrace();                }赶上(IOException异常五){                    e.printStackTrace();                }               //Looper.getMainLooper()退出()。        返回null;        }@覆盖保护无效onPostExecute(虚空结果){     main.populateAlbumVersionsView();     main.progressBar.setVisibility(View.GONE);}} 
ArrayList在多线程时抛出ConcurrentModificationException异常的原因和解决方法

解决方案

活套无关与循环。活套是Android系统,控制UI线程的一部分。有几件事情,不会工作没有preparing弯针,但一般最好避免活套prepare()。除非是绝对nessisary。这是迄今为止最好使用异步doInBackground执行数据处理或其他较长的操作,然后更新onPostExecute和onProgressUpdate的用户界面。

总之,除非你正在使用某种方式的界面,你并不需要调用活套。如果你发现自己不得不叫尺蠖,你可能需要调整您如何后台线程正在工作。

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);
}   
}

解决方案

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 absolutly nessisary. 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.