1

我正在尝试加载图像以显示在列表视图中的文本旁边。由于没有异步任务的滚动很糟糕,我正在尝试实现它。不幸的是,我的代码在 onPostExecute() 中给了我空指针错误,我似乎无法找出原因。我的代码如下:

class LoadImage extends AsyncTask<Object, Void, Bitmap>{

    private ImageView imv;
    private Object imageViewHolder;
    private String position;
    private Object path;

    public LoadImage(ImageView imv, String _position) {
        Log.w("MyApp", "creating LoadImage");
        this.imv = imv;
        Log.w("MyApp", "got image view");
        path = imv.getTag();
        Log.w("MyApp", "Got Imageview Path");
        position = _position;
        Log.w("MyApp", "LoadImage created");
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        Log.w("MyApp", "in doInBackground");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Log.w("MyApp", "made bitmap factory");

        Bitmap bm = null;

        Log.w("MyApp", "Getting URL ID");
        File dir = new File(PATH + position);
        Log.w("MyApp", "Have URL ID");

        if(dir.isDirectory()){
            Log.w("MyApp","Dir is a directory");

            File header = new File(dir.getAbsolutePath() + "/title");
            Log.w("MyApp", "Checking for title");
            if(header.isFile()){
                bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
            }

            if(bm!=null){
                Log.w("MyApp", "Title is good");
            }else{
                Context c = ReadingList.this;
                bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
            }
        }else{
            Log.w("MyApp", "Dir Not Directory");
            Context c = ReadingList.this;
            bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
        }


        return bm;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        if (!imv.getTag().equals(path)) {
            Log.w("MyApp", "Not setting images");
            return;
        }

        if(result != null && imv != null){
            Log.w("MyApp", "setting bitmap");
            imv.setImageBitmap(result);
        }else{
            Log.w("MyApp", "Nothing happened");
        }
    }

}

我要解决的问题是不让位图与回收的视图一起回收。我查看了此站点上的一些示例,但不确定为什么这不起作用。从这个问题的回答中借用了很多代码。onPostExecute 不会在日志中输入任何内容,也不会立即崩溃。崩溃似乎需要大约两分钟。图像是从 SD 卡中提取的,所以我不确定为什么要花这么长时间来决定它是否要崩溃。

in doInBackground
made bitmap factory
Getting URL ID
Have URL ID
Dir is a directory
Checking for title
Title is good
in doInBackground
made bitmap factory
getting URL ID
Have URL ID
Dir is a directory
Checking title
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:305)
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:1)

在此之后它继续重复前 7 行多次,但应用程序崩溃了,我看不到它们是否有任何作用。然而,onPostExecute 似乎再也没有被调用过。

以供参考:

304: if (!imv.getTag().equals(path)) {
305:    Log.w("MyApp", "Not setting images");
306:    return;
307: }
4

1 回答 1

1

我找到了一个适合我的解决方案。我使用了aQuery,并使用了他们提供的文件中的 shouldDelay 和异步集图像。

if(header.isFile() && !aq.shouldDelay(position, convertView, parent, 
   header.getAbsolutePath())){
     bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
     showcase.setImageBitmap(bm);
 } else {                    
     showcase.setImageResource(R.drawable.missing_image);
 }
于 2012-04-30T16:59:28.603 回答