【问题标题】:Loading images in listview在列表视图中加载图像
【发布时间】:2012-04-10 02:13:16
【问题描述】:

我正在尝试加载图像以显示在列表视图中的文本旁边。由于没有异步任务的滚动很糟糕,我正在尝试实现它。不幸的是,我的代码在 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");
        }
    }

}

我要解决的问题是不让位图与回收的视图一起回收。我查看了此站点上的一些示例,但不确定为什么这不起作用。从question 的回复中借用了很多代码。 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: }

【问题讨论】:

  • 请放上logcat,以便我们更具体地为您提供帮助。
  • 添加了 logcat 和引用的行。希望有帮助。不确定是什么时候抛出了空指针异常。 onPostExecute 看起来在 imv 和 path 的范围内,我看不出两者都为空。
  • 评论第 305 行,然后看看发生了什么?
  • 我已经做到了。那时,我在滚动时一遍又一遍地得到相同的图像集,当停止时,图像开始疯狂地循环,因为多个异步任务正在处理一个图像视图,因为视图回收。 304-307 中的 if 语句应该有助于防止这种情况发生。至少这是目标。相反,我在滚动后数十次看到相同的 Imageview 设置为不同的图像。
  • 尝试清理项目,然后再次运行

标签: android listview android-asynctask


【解决方案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-07-25
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多