在android上从asyncTask获取java.util.concurrent.RejectedExecutionException

在android上从asyncTask获取java.util.concurrent.RejectedExecutionException,android,xml,multithreading,layout,android-asynctask,Android,Xml,Multithreading,Layout,Android Asynctask,我正在将sqlite数据库读入tableLayout。我不想在一个单独的线程中执行此操作,而不是在没有ui更新的情况下等待很长时间。因此,我使用AsyncTask来完成一些工作并发布结果。然而,在我的列表中,只有大约1/4的项目真正进入了TableLayout。它在没有异步任务的情况下运行良好。列表中的大多数项都会抛出一个错误(我发现了),java.util.concurrent.RejectedExecutionException。我不知道这是为什么。这是我的密码 myDB.execSQL("

我正在将sqlite数据库读入tableLayout。我不想在一个单独的线程中执行此操作,而不是在没有ui更新的情况下等待很长时间。因此,我使用AsyncTask来完成一些工作并发布结果。然而,在我的列表中,只有大约1/4的项目真正进入了TableLayout。它在没有异步任务的情况下运行良好。列表中的大多数项都会抛出一个错误(我发现了),java.util.concurrent.RejectedExecutionException。我不知道这是为什么。这是我的密码

myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + TableName
                + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");

        Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);         

        c.moveToFirst();
        if (c != null) {
            int color = 0xFFdfe8ea;
            this.startManagingCursor(c);
            // Loop through all Results
            do {
                try{
                    MyAsyncTask aTask = new MyAsyncTask();
                    String[]strings= {c.getString(c.getColumnIndex("title")),c.getString(c.getColumnIndex("artist")),c.getString(c.getColumnIndex("time")),c.getString(c.getColumnIndex("album")),""+color};
                    aTask.execute(strings);
                }catch(Exception e){
                    Log.w("****", e);
                }
        if (color == 0xFFdfe8ea) {
                    color = 0xFFf2f8fa;
                } else {
                    color = 0xFFdfe8ea;
                }
            } while (c.moveToNext());
        }

    } catch (SQLException e) {
        Log.e("****", e.toString());
    } finally {
        if (myDB != null) {
            myDB.close();
        }
    }
下面是异步任务

class MyAsyncTask extends AsyncTask<String, Void, View> {
    @Override
    protected View doInBackground(String... params) {
        int color = Integer.parseInt(params[4]);

        TableRow tr = new TableRow(MainActivity.this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView space = new TextView(MainActivity.this);
        space.setText("");
        space.setBackgroundColor(color); //0xFFf2f8fa alternating
        space.setSingleLine();
        space.setPadding(2, 2, 2, 2);
        space.setGravity(Gravity.LEFT);
        space.setTextColor(0xFF000000);
        space.setLayoutParams(new LayoutParams(
                findViewById(R.id.spaceColumn).getWidth(),
                LayoutParams.WRAP_CONTENT));

        /* Create a Button to be the row-content. */
        TextView title = new TextView(MainActivity.this);
        title.setText(params[0]);
        title.setBackgroundColor(color); //0xFFf2f8fa alternating
        title.setSingleLine();
        title.setPadding(2, 2, 2, 2);
        title.setGravity(Gravity.LEFT);
        title.setTextColor(0xFF000000);
        title.setEllipsize(TruncateAt.END);
        title.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Create a Button to be the row-content. */
        TextView artist = new TextView(MainActivity.this);
        artist.setText(params[1]);
        artist.setBackgroundColor(color); //0xFFf2f8fa alternating
        artist.setSingleLine();
        artist.setPadding(2, 2, 2, 2);
        artist.setGravity(Gravity.LEFT);
        artist.setTextColor(0xFF000000);
        artist.setEllipsize(TruncateAt.END);
        artist.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Create a Button to be the row-content. */
        TextView time = new TextView(MainActivity.this);
        time.setText(params[2]);
        time.setBackgroundColor(color); //0xFFf2f8fa alternating
        time.setSingleLine();
        time.setPadding(2, 2, 2, 2);
        time.setGravity(Gravity.LEFT);
        time.setTextColor(0xFF000000);
        time.setLayoutParams(new LayoutParams(
                findViewById(R.id.timeColumn).getWidth(),
                LayoutParams.WRAP_CONTENT));

        /* Create a Button to be the row-content. */
        TextView album = new TextView(MainActivity.this);
        album.setText(params[3]);
        album.setBackgroundColor(color); //0xFFf2f8fa alternating
        album.setSingleLine();
        album.setPadding(2, 2, 2, 2);
        album.setGravity(Gravity.LEFT);
        album.setTextColor(0xFF000000);
        album.setEllipsize(TruncateAt.END);
        album.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Add Button to row. */
        tr.addView(space);
        tr.addView(title);
        tr.addView(artist);
        tr.addView(time);
        tr.addView(album);

        /* Add row to TableLayout. */
        return tr;
    }

    @Override
    protected void onPostExecute(View tr) {
        ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void onPreExecute() {
    }
 }
类MyAsyncTask扩展了AsyncTask{ @凌驾 受保护视图doInBackground(字符串…参数){ int color=Integer.parseInt(参数[4]); TableRow tr=新的TableRow(MainActivity.this); tr.setLayoutParams(新的LayoutParams( LayoutParams.FILL\u父级, LayoutParams.WRAP_内容); TextView空间=新的TextView(MainActivity.this); 空格.setText(“”); space.setBackgroundColor(color);//0xFFf2f8fa space.setSingleLine(); 设置填充(2,2,2,2); space.setGravity(Gravity.LEFT); space.setTextColor(0xFF000000); space.setLayoutParams(新的LayoutParams( findViewById(R.id.spaceColumn).getWidth(), LayoutParams.WRAP_内容); /*创建一个按钮作为行内容*/ TextView title=新的TextView(MainActivity.this); title.setText(参数[0]); title.setBackgroundColor(颜色);//0xFFf2f8fa title.setSingleLine(); 标题.设置填充(2,2,2,2); 标题.设置重力(重力.左); title.setTextColor(0xFF000000); title.setEllipsize(TruncateAt.END); 标题.setLayoutParams(新的LayoutParams( 0, LayoutParams.WRAP_内容,1); /*创建一个按钮作为行内容*/ TextView艺术家=新的TextView(MainActivity.this); 艺术家setText(参数[1]); 艺术家.setBackgroundColor(颜色);//0xFFf2f8fa 艺术家:setSingleLine(); 艺术家设置填充(2,2,2,2); 艺术家。设置重力(重力。左); 艺术家:setTextColor(0xFF000000); 艺术家:setEllipsize(TruncateAt.END); 艺术家setLayoutParams(新的LayoutParams( 0, LayoutParams.WRAP_内容,1); /*创建一个按钮作为行内容*/ TextView时间=新的TextView(MainActivity.this); time.setText(参数[2]); time.setBackgroundColor(颜色);//0xFFf2f8fa time.setSingleLine(); 设置填充时间(2,2,2,2); 时间。设置重力(重力。左); time.setTextColor(0xFF000000); time.setLayoutParams(新的LayoutParams( findViewById(R.id.timeColumn).getWidth(), LayoutParams.WRAP_内容); /*创建一个按钮作为行内容*/ TextView相册=新的TextView(MainActivity.this); album.setText(params[3]); album.setBackgroundColor(颜色);//0xFFf2f8fa album.setSingleLine(); 设置填充(2,2,2,2); 相册.设置重力(重力.左); album.setTextColor(0xFF000000); album.setEllipsize(TruncateAt.END); album.setLayoutParams(新的LayoutParams( 0, LayoutParams.WRAP_内容,1); /*将按钮添加到行*/ tr.addView(空间); tr.addView(标题); tr.addView(艺术家); tr.addView(时间); tr.addView(相册); /*将行添加到TableLayout*/ 返回tr; } @凌驾 受保护的void onPostExecute(查看tr){ ((TableLayout)findViewById(R.id.TableLayout)).addView(tr,new TableLayout.LayoutParams( LayoutParams.FILL\u父级, LayoutParams.WRAP_内容); } @凌驾 受保护的void onPreExecute(){ } }
作为参考,这是我如何修复它的

class MyAsyncTask extends AsyncTask<Void, Song, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        SQLiteDatabase myDB = openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
        String TableName = "songs";

        myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + TableName
                + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");

        Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);

        c.moveToFirst();
        int filepathIndex=c.getColumnIndex("filepath");
        int titleIndex=c.getColumnIndex("title");
        int artistIndex=c.getColumnIndex("artist");
        int albumIndex=c.getColumnIndex("album");
        int timeIndex=c.getColumnIndex("time");
        int playcountIndex=c.getColumnIndex("playcount");

        if (c != null) {
            int color = 0xFFdfe8ea;
         //   this.startManagingCursor(c);
            // Loop through all Results
            do {
                Song song = new Song(c.getString(filepathIndex),c.getString(titleIndex),c.getString(artistIndex),c.getString(albumIndex),c.getString(timeIndex),c.getInt(playcountIndex),color);
                // Add to song the data from your cursor
                publishProgress(song);

                if (color == 0xFFdfe8ea) {
                    color = 0xFFf2f8fa;
                } else {
                    color = 0xFFdfe8ea;
                }
            } while (c.moveToNext());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void item) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Song... items) {
        for (Song song : items) {
            TableRow tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView space = new TextView(MainActivity.this);
            space.setText("");
            space.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            space.setSingleLine();
            space.setPadding(2, 2, 2, 2);
            space.setGravity(Gravity.LEFT);
            space.setTextColor(0xFF000000);
            space.setLayoutParams(new LayoutParams(
                    findViewById(R.id.spaceColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView title = new TextView(MainActivity.this);
            title.setText(song.title);
            title.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            title.setSingleLine();
            title.setPadding(2, 2, 2, 2);
            title.setGravity(Gravity.LEFT);
            title.setTextColor(0xFF000000);
            title.setEllipsize(TruncateAt.END);
            title.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView artist = new TextView(MainActivity.this);
            artist.setText(song.artist);
            artist.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            artist.setSingleLine();
            artist.setPadding(2, 2, 2, 2);
            artist.setGravity(Gravity.LEFT);
            artist.setTextColor(0xFF000000);
            artist.setEllipsize(TruncateAt.END);
            artist.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView time = new TextView(MainActivity.this);
            time.setText(song.time);
            time.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            time.setSingleLine();
            time.setPadding(2, 2, 2, 2);
            time.setGravity(Gravity.LEFT);
            time.setTextColor(0xFF000000);
            time.setLayoutParams(new LayoutParams(
                    findViewById(R.id.timeColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView album = new TextView(MainActivity.this);
            album.setText(song.album);
            album.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            album.setSingleLine();
            album.setPadding(2, 2, 2, 2);
            album.setGravity(Gravity.LEFT);
            album.setTextColor(0xFF000000);
            album.setEllipsize(TruncateAt.END);
            album.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Add Button to row. */
            tr.addView(space);
            tr.addView(title);
            tr.addView(artist);
            tr.addView(time);
            tr.addView(album);

            // Add the row to the table
            ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }
}
类MyAsyncTask扩展了AsyncTask{ @凌驾 受保护的Void doInBackground(Void…参数){ SQLiteDatabase myDB=openOrCreateDatabase(“DatabaseName”,MODE_PRIVATE,null); String TableName=“歌曲”; myDB.execSQL(“如果不存在,则创建表” +表名 +“(_idinteger主键、文件路径文本唯一、标题文本、艺术家文本、专辑文本、时间文本、播放计数数字);”; 游标c=myDB.rawQuery(“SELECT*FROM”+TableName,null); c、 moveToFirst(); int filepathIndex=c.getColumnIndex(“filepath”); int titleIndex=c.getColumnIndex(“标题”); int artistIndex=c.getColumnIndex(“艺术家”); int albumIndex=c.getColumnIndex(“唱片”); int timeIndex=c.getColumnIndex(“时间”); int playcountIndex=c.getColumnIndex(“playcount”); 如果(c!=null){ int color=0xFFdfe8ea; //这个.startManagingCursor(c); //循环浏览所有结果 做{ 歌曲=新歌(c.getString(filepathIndex)、c.getString(titleIndex)、c.getString(artistIndex)、c.getString(albumIndex)、c.getString(timeIndex)、c.getInt(playcountIndex)、颜色); //将光标中的数据添加到歌曲中 出版进度(歌曲); 如果(颜色==0xFFdfe8ea){ 颜色=0xFFf2f8fa; }否则{ 颜色=0xFFdfe8ea; } }而(c.moveToNext()); } 返回null; } @凌驾 受保护的void onPostExecute(无效项){ } @凌驾 受保护的void onPreExecute(){ } @凌驾 受保护的void onProgressUpdate(歌曲…项目){ 用于(歌曲:项目){ TableRow tr=新的TableRow(MainActivity.this); tr.setLayoutParams(新的LayoutParams( 放置
public class MyAdapter extends BaseAdapter {

    private Context context = null;
    private Cursor cursor;

    public MyAdapter(Context context){

        this.context = context;     
        SQLiteDatabase db = DatabaseHelper.getInstance(context).getReadableDatabase();              
        this.cursor = db.query("YOUR QUERY");

    }

    @Override
    public int getCount() {
        return this.cursor.getCount();
    }


    public Cursor getCursor() {
        return cursor;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout row;

        try {           
        cursor.moveToPosition(position);

        if (convertView == null) {      
            row = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.myRowLayout, parent, false);
            } else {
            row = (LinearLayout) convertView;
            }

        TextView name = (TextView) row.findViewById(R.id.myLayoutId);
        name.setText(cursor.getString(cursor.getColumnIndex("your column")));

        } catch (Exception e) {
            row = null;
            Log.e(LOG_TAG, "" + e.getMessage());
            e.printStackTrace();
        }

        return row;

    }

    @Override
    public MoneyCurrency getItem(int position) {        
        this.cursor.moveToPosition(position);
        long id = this.cursor.getLong(this.cursor.getColumnIndex("your column id")); 
        return new Object.read(id, context, null); //Return whatever you want to show in that row. This is used if you want to use onClick listeners or menus
    }

    @Override
    public long getItemId(int position) {
        this.cursor.moveToPosition(position);
        return this.cursor.getLong(this.cursor.getColumnIndex("your id column"));
    }



    }
class MyAsyncTask extends AsyncTask<Void, Song, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        myDB.execSQL("CREATE TABLE IF NOT EXISTS "
            + TableName
            + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");

        Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);         

        c.moveToFirst();
        if (c != null) {
            int color = 0xFFdfe8ea;
            this.startManagingCursor(c);
            // Loop through all Results
            do {
                Song song = new Song();
                // Add to song the data from your cursor
                publishProgress(song);
            } while (c.moveToNext());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void item) {
    }

    @Override
    protected void onPreExecute() {
    }       

    @Override
    protected void onProgressUpdate(Song... items) {
        for (Song song : items) {    
            TableRow tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView space = new TextView(MainActivity.this);
            space.setText("");
            space.setBackgroundColor(color); //0xFFf2f8fa alternating
            space.setSingleLine();
            space.setPadding(2, 2, 2, 2);
            space.setGravity(Gravity.LEFT);
            space.setTextColor(0xFF000000);
            space.setLayoutParams(new LayoutParams(
                    findViewById(R.id.spaceColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView title = new TextView(MainActivity.this);
            title.setText(song.getTitle());
            title.setBackgroundColor(color); //0xFFf2f8fa alternating
            title.setSingleLine();
            title.setPadding(2, 2, 2, 2);
            title.setGravity(Gravity.LEFT);
            title.setTextColor(0xFF000000);
            title.setEllipsize(TruncateAt.END);
            title.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView artist = new TextView(MainActivity.this);
            artist.setText(song.getArtist());
            artist.setBackgroundColor(color); //0xFFf2f8fa alternating
            artist.setSingleLine();
            artist.setPadding(2, 2, 2, 2);
            artist.setGravity(Gravity.LEFT);
            artist.setTextColor(0xFF000000);
            artist.setEllipsize(TruncateAt.END);
            artist.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView time = new TextView(MainActivity.this);
            time.setText(song.getTime());
            time.setBackgroundColor(color); //0xFFf2f8fa alternating
            time.setSingleLine();
            time.setPadding(2, 2, 2, 2);
            time.setGravity(Gravity.LEFT);
            time.setTextColor(0xFF000000);
            time.setLayoutParams(new LayoutParams(
                    findViewById(R.id.timeColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView album = new TextView(MainActivity.this);
            album.setText(song.getAlbum());
            album.setBackgroundColor(color); //0xFFf2f8fa alternating
            album.setSingleLine();
            album.setPadding(2, 2, 2, 2);
            album.setGravity(Gravity.LEFT);
            album.setTextColor(0xFF000000);
            album.setEllipsize(TruncateAt.END);
            album.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Add Button to row. */
            tr.addView(space);
            tr.addView(title);
            tr.addView(artist);
            tr.addView(time);
            tr.addView(album);

            // Add the row to the table
            ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
        }
    }
 }
private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;
private static final int KEEP_ALIVE = 1;

private static final BlockingQueue<Runnable> sPoolWorkQueue =
      new LinkedBlockingQueue<Runnable>(10);

/**
* An {@link Executor} that can be used to execute tasks in parallel.
*/
    public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);