2012-04-10 9 views
1

画像を読み込んで、リストビューのテキストの隣に表示しようとしています。スクロールはasynctaskなしでひどいので、私はそれを実装しようとしています。残念ながら、私のコードは、私にonPostExecute()でnullポインタエラーを与えています、なぜ私は見つけることができません。リストビューに画像をロードする

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はログに何も入力せず、即座にクラッシュしません。クラッシュするのに約2分かかるようです。画像が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を入れてください。 –

+0

logcatと参照される行が追加されました。希望が役立ちます。しかし、nullポインタ例外がスローされているかどうかはわかりません。 onPostExecuteはimvとpathの両方のスコープにあるように見えますが、いずれかがnullになることはありません。 –

+0

行305にコメントして、何が起こったのか見てみましょうか? –

答えて

1

私は私の作品解決策を見つけました。私はaQueryを使い、彼らが提供するファイルからshouldDelayとasyncのセットイメージを使用しました。

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); 
} 
関連する問題