2012-11-06 90 views
-1

我有一個問題,我無法解決qal。 我試過所有的方法。 這是AsyncTask中的一個問題,它不應該是正確的。AsyncTask畫廊與網絡圖像

是否有人demosntrar我的正確方法。

public class ExibeGallery extends SherlockActivity { 
    private Gallery galleryView1; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.exibegallery); 
     Log.i("19", "Chama a gallery");   
     AdaptadordaImagem adp = new AdaptadordaImagem(this); 
     galleryView1 = (Gallery) findViewById(R.id.GaleryView); 
     galleryView1.setAdapter(adp); 
    } 
} 

public class AdaptadordaImagem extends BaseAdapter { 
    private Context cont; 
    int GalItemBg; 
    private String[] myRemoteImages = { 
     "http://www.cuboweb.com.br/android/images/1.png", 
     "http://www.cuboweb.com.br/android/images/2.png", 
     "http://www.cuboweb.com.br/android/images/3.png", 
     "http://www.cuboweb.com.br/android/images/4.png", }; 
     static Bitmap bm; 
     private int[] img = {0, 1, 2, 3}; 
     BitmapFactory.Options bmOptions; 
     private Object mGalleryItemBackground; 
     ImageView i = null; 

    public AdaptadordaImagem(Context c) { 
    cont = c; 

    i = new ImageView(this.cont); 
    PegaImagens pg = new PegaImagens(); 
    pg.execute(); 
    Log.i("1", "Adaptador de imagem"); 
    } 

     private TypedArray obtainStyledAttributes(int[] hellogallery) { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     public int getCount() { 
      return this.img.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      Log.i("2", "getView"); 
      ImageView i = new ImageView(cont); 
      i.setBackgroundResource(img[position]); 
      i.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
      i.setBackgroundResource((Integer) mGalleryItemBackground); 
      i.setAdjustViewBounds(true); 
      return i; 
     } 

     public class PegaImagens extends AsyncTask<String[], Void, String> { 
      private ProgressDialog progressgallery; 

      protected void onPreExecute() { 
       Log.i("3", "AsyncTask onPreexecute"); 
       progressgallery = new ProgressDialog(cont, GalItemBg); 
       progressgallery.setTitle("Aguarde..."); 
       progressgallery.setMessage("Carregando..."); 
       progressgallery.show(); 
      } 

      public Bitmap loadBitmap(String[] myRemoteImages, BitmapFactory.Options options) { 
       Log.i("4", "AsyncTask loadBitmap"); 
       InputStream in = null; 
       int i; 
       for (i = 0; i <= 3; i++) { 
        Log.i("5", "AsyncTask loadBitmap for"); 
        try { 
         Log.i("5", "AsyncTask loadBitmap for try"); 
         in = OpenHttpConnection(myRemoteImages[i]); 
         bm = BitmapFactory.decodeStream(in, null, options); 
         in.close(); 
        } catch (IOException e1) { 
         Log.i("6", "AsyncTask loadBitmap for catch"); 
         bm = null; 
        } 
       } 
       return bm; 
      }// aqui fecha o load bitmap 

      private InputStream OpenHttpConnection(String myRemoteImages) throws IOException { 
       Log.i("6", "AsyncTask OpenHttpConnection"); 
       InputStream inputStream = null; 
       URL url = new URL(myRemoteImages); 
       URLConnection conn = url.openConnection(); 
       try { 
        Log.i("7", "AsyncTask OpenHttpConnection try"); 
        HttpURLConnection httpConn = (HttpURLConnection) conn; 
        httpConn.setRequestMethod("GET"); 
        httpConn.connect(); 
        InputStream is = conn.getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(is); 
        Bitmap bm = BitmapFactory.decodeStream(bis); 
        bis.close(); 
        is.close(); 
        i.setImageBitmap(bm); 
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
         Log.i("8", "AsyncTask OpenHttpConnection try if"); 
         inputStream = httpConn.getInputStream(); 
        } 
       } catch (Exception ex) { 
        Log.i("9", "AsyncTask OpenHttpConnection catch"); 
        i.setImageResource(R.drawable.sem_conexao); 
       } 
       return inputStream; 
      }// aqui fecha o InputStream 

      protected String doInBackground(String[]... params) { 
       // TODO Auto-generated method stub 
       Log.i("10", "AsyncTask doInBackground"); 
       int x; 
       for (x = 0; x <= 3; x++) { 
        Log.i("11", "AsyncTask doInBackground for"); 
        try { 
         Log.i("12", "AsyncTask doInBackground for try"); 
         bmOptions = new BitmapFactory.Options(); 
         bmOptions.inSampleSize = 1; 
        } catch (ArrayIndexOutOfBoundsException erro) { 
         Log.i("13", "AsyncTask doInBackground for try catch1"); 
         myRemoteImages[x] = ""; 
        } catch (Exception erro) { 
         Log.i("14", "AsyncTask doInBackground for try catch2"); 
         myRemoteImages[x] = ""; 
        } 
       } 
       loadBitmap(myRemoteImages, bmOptions); 
       return myRemoteImages[x]; 
      } 

      protected void onPostExecute(String[] myRemoteImages) { 
       Log.i("15", "AsyncTask onPostExecute "); 
       if (!myRemoteImages.equals("")) { 
        // ok 
        Log.i("16", "AsyncTask onPostExecute if ok "); 
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
        mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); 
        a.recycle(); 
       } else { 
        Log.i("17", "AsyncTask onPostExecute else falha "); 
       } 
       progressgallery.dismiss(); 
      } 
     }// end async task 
    }// end class 

錯誤日誌: -

11-07 11:51:12.865: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
11-07 11:51:12.875: E/AndroidRuntime(529): FATAL EXCEPTION: main 
11-07 11:51:12.875: E/AndroidRuntime(529): java.lang.NullPointerException 
11-07 11:51:12.875: E/AndroidRuntime(529): at br.com.consulfarma.acs.AdaptadordaImagem.getView(AdaptadordaImagem.java:71) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:956) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.onMeasure(LinearLayout.java:521) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) 
11-07 11:51:12.875: E/AndroidRuntime(529): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.View.measure(View.java:10828) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewRoot.performTraversals(ViewRoot.java:909) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:132) 
11-07 11:51:12.875: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4025) 
11-07 11:51:12.875: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method) 
11-07 11:51:12.875: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:491) 
11-07 11:51:12.875: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
11-07 11:51:12.875: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
11-07 11:51:12.875: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method) 
+0

發佈您的logcat輸出 –

+0

已發佈。 –

+0

@Flip_novidade你是否在Manifest中聲明瞭你的活動? – sinisha

回答

0

失敗是以下行:

ImageView i = new ImageView(this.cont); 

此行添加到構造器,像這樣:

//this line edited; 
ImageView i = null; 

public AdaptadordaImagem(Context c) { 
    cont = c; 

    //this line added 
    i = new ImageView(this.cont); 

    PegaImagens pg = new PegaImagens(); 
    pg.execute(); 
    Log.i("1", "Adaptador de imagem"); 
} 

或者甚至刪除它,因爲似乎不是真正用在你的代碼中(在getView方法中創建新的ImageView)。

希望它有幫助。 Saludos

+0

我明白了,大部分還是有問題 –

+0

我還是有問題的。 我沒有。 我可以證明。 –

+0

我的問題是在InputStream OpenHttpConnection –

0

它看起來像你的IMG從不初始化,至少在發佈的代碼。如果是這樣,getCount()應該是null。

如果這是真的,你需要這樣的:

private int[] img = someIntArrayWithYourIds; 

編輯:澄清:

嘗試在此行中getCount將設置一個調試器斷點,看到IMG和IMG的值.length:

 return this.img.length; 

機會是,img在那裏是空的。

+0

我不明白。我沒有。我可以證明。 –

+0

編輯答案:看起來像img未初始化,因此爲空。 –

+0

我仍然有問題... 有人可以幫助我 –