使用asyncTask加载ListView的位图时RejectedExecutionException

时间:2014-07-02 12:33:38

标签: java android android-listview android-asynctask android-bitmap

我正在尝试使用按需生成的位图创建ListView。我正在使用https://github.com/chrisbanes/Android-BitmapCache缓存位图

我正在使用SimpleAdapter,其中包含:

setViewBinder(new SimpleAdapter.ViewBinder() {
 @Override
 public boolean setViewValue(View view, Object object,String string) {
     if( string.startsWith("ClassName:") ){
         final ImageView yourImageView=(ImageView) view;
         AsyncTask<String, Void, Bitmap> imageLoadAsyncTask = new AsyncTask<String, Void, Bitmap>() {
             String cname;
             @Override
             protected Bitmap doInBackground(String... classnames) {
                 cname=classnames[0];
                 return db.getCharIcon(classnames[0]);
             }

             @Override
             protected void onPostExecute(Bitmap bitmap) {
                 yourImageView.setImageBitmap(bitmap);
             }
         };
         imageLoadAsyncTask.execute(string.substring(TextUtils.getTrimmedLength("ClassName:")));
         return true;
     }
     return false;
    }
}

getCharIcon函数在这里:

Bitmap getCharIcon(String classname){
    Bitmap modBmp;
    CacheableBitmapDrawable cacheBmp=mCache.get(classname);
    if(cacheBmp==null)
    {
        modBmp=createCharIcon(classname);
        mCache.put(classname,modBmp);
    }
    else
    {
        modBmp=cacheBmp.getBitmap();
    }
    return modBmp;
}

其中createCharIcon以这种方式生成位图(我还没有实现选择图像的好部分的最终版本):

Bitmap createCharIcon(String classname)
{
    Bitmap modBmp = Bitmap.createBitmap(srcBmp,0,0,60,60);
    return modBmp;
}

可悲的是我收到了这个错误:

07-02 14:13:04.248  11592-11592/com.lectem.gecharacters E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.util.concurrent.RejectedExecutionException
            at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1876)
            at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:774)
            at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1295)
            at android.os.AsyncTask.execute(AsyncTask.java:394)
            at com.lectem.gecharacters.CharFragment$1.setViewValue(CharFragment.java:110)
            at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:168)
            at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
            at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
            at android.widget.AbsListView.obtainView(AbsListView.java:1294)
            at android.widget.ListView.makeAndAddView(ListView.java:1730)
            at android.widget.ListView.fillDown(ListView.java:655)
            at android.widget.ListView.fillSpecific(ListView.java:1287)
            at android.widget.ListView.layoutChildren(ListView.java:1573)
            at android.widget.AbsListView.onLayout(AbsListView.java:1147)
            at android.view.View.layout(View.java:7035)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
            at android.view.View.layout(View.java:7035)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
            at android.view.View.layout(View.java:7035)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
            at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1241)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
            at android.view.View.layout(View.java:7035)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
            at android.view.View.layout(View.java:7035)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1045)
            at android.view.View.layout(View.java:7035)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
            at android.view.View.layout(View.java:7035)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
            at android.view.View.layout(View.java:7035)
            at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:4627)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:521)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
            at dalvik.system.NativeStart.main(Native Method)

当我将列表滚回太快时,我收到此错误,因此我认为它与我可以使用的asyncTasks的数量相关联... 我真的不确定这是不是应该怎么做,但是bitmapLruCache库需要在主线程之外运行。

1 个答案:

答案 0 :(得分:2)

通常,该错误意味着您尝试一次使用过多的AsyncTasks。在AdapterView中,如果您为每个项目(例如,AsyncTask中的每一行)分配ListView而不考虑行回收,并且用户将列表移动,则会发生这种情况

引用the library's documentation

  

如果您希望库和回收功能正常工作,您必须尽可能使用捆绑的CacheableImageView。

由于您似乎在AdapterView中使用此功能,请确保您使用的是CacheableImageView