2012-04-10 161 views
1

我正试图加载图像以显示在列表视图中的文本旁边。由于滚动是可怕的没有一个asynctask,我试图实现这一点。不幸的是,我的代码给了我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: } 
+0

请把logcat放在里面,这样我们可以更具体地帮助你。 – 2012-04-10 06:33:00

+0

添加了logcat和引用的行。希望有所帮助。不知道是否正在抛出空指针异常。 onPostExecute看起来是在imv和路径的范围内,我看不出如何是null。 – 2012-04-10 07:17:32

+0

评论305行,然后看看发生了什么? – 2012-04-10 07:22:20

回答

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); 
} 
相关问题