2012-01-21 96 views
0

我有3個按鈕和一個Gallery.In我的畫廊我有3個ImageAdapters一個爲每個按鈕的點擊我設置它必須讀取並加載到這些縮略圖網址ImageAdapters和我正在使用AsyncTask。致命異常:#的AsyncTask 2

代碼:

class AddImageTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected Void doInBackground(Void... unused) { 
     try { 
     for (String url : PhotoURLS) { 
     String filename = url.substring(url.lastIndexOf("/") + 
     1,url.length()); 
     filename = "" + filename; 
     String thumburl = url.substring(0, url.lastIndexOf("/") + 1); 
     imageAdapter.addItem(LoadThumbnailFromURL(thumburl+ filename)); 
     publishProgress(); 
     } 

     for (String url : AudioThumbURLS) { 
     String filename = url.substring(url.lastIndexOf("/") + 
     1,url.length()); 
     filename = "" + filename; 
     String thumburl = url.substring(0, url.lastIndexOf("/") + 1); 
     imageAdapter1.addItem(LoadThumbnailFromURL(thumburl+ filename)); 
     publishProgress(); 
     } 
     for (String url : VideoThumbURLS) { 
     String filename = url.substring(url.lastIndexOf("/") + 
     1,url.length()); 
     filename = "" + filename; 
     String thumburl = url.substring(0, url.lastIndexOf("/") + 1); 
     imageAdapter2.addItem(LoadThumbnailFromURL(thumburl+ filename)); 
     publishProgress(); 
     } 

     } catch (Exception ae) { 
     ae.printStackTrace(); 
     Toast.makeText(getApplicationContext(),"Error found in the application", 
     Toast.LENGTH_LONG).show(); 
     } 
     return (null); 
    } 

    @Override 
    protected void onProgressUpdate(Void... unused) { 
     imageAdapter.notifyDataSetChanged(); 
     imageAdapter1.notifyDataSetChanged(); 
     imageAdapter2.notifyDataSetChanged(); 
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
    } 
} 

而且我LoadThumbnailFromURL方法是:

private Drawable LoadThumbnailFromURL(String url) { 
    try { 
     URLConnection connection = new URL(url).openConnection(); 
     String contentType = connection.getHeaderField("Content-Type"); 
     boolean isImage = contentType.startsWith(""); 
     if (isImage) { 
      HttpGet httpRequest = new HttpGet(url); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response = (HttpResponse) httpclient 
        .execute(httpRequest); 
      HttpEntity entity = response.getEntity(); 
      BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
        entity); 
      InputStream is = bufferedHttpEntity.getContent(); 
      Drawable d = Drawable.createFromStream(is, "src name"); 
      return d; 
     } else { 
      Bitmap b = BitmapFactory.decodeResource(getResources(), 
        R.drawable.icon); 
      Drawable d = new BitmapDrawable(b); 
      return d; 
     } 
    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG) 
       .show(); 
     Log.e(e.getClass().getName(), e.getMessage(), e); 

    } 
    return null; 
} 

應用程序啓動時不錯,但之後的某個拋出錯誤爲:

01-21 15:16 :23.750:E/AndroidRuntime(8292):致命異常:的AsyncTask#2 01-21 15:16:23.750:E/AndroidRuntime(8292):在JAV a.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:273) 01-21 15:16:23.750:E/AndroidRuntime(8292):在java.util.concurrent.FutureTask.setException(FutureTask.java: 124) 01-21 15:16:23.750:E/AndroidRuntime(8292):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307) 01-21 15:16:23.750:E/AndroidRuntime(8292):在java.util.concurrent.FutureTask.run(FutureTask.java:137) 01-21 15:16:23.750:E/AndroidRuntime(8292):在java.util.concurrent.FutureTask中$同步。 innerRun(FutureTask.java:305)

可能的原因是什麼?如何解決?

+0

你可以嘗試僅在更新的適配器上調用notifyDataSetChanged(),看看是否是這個問題。 – Cata

+0

你的意思是說我需要在'onPostExecute'方法內調用'notifyDataSetChanged()'? –

+0

nope,我的意思是通過onPostExecute()方法傳遞一個整數或其他信息來知道哪個適配器已更新並只更新該特定的適配器 – Cata

回答

3

任何機會,你是捕獲異常,但進入內部你釣到的魚的另一個異常{}?由於它位於不同的(非UI)線程上,因此不要嘗試使用Toast來通知您自己的錯誤。