AsyncTask Gallery与来自网络的图像

时间:2012-11-06 12:15:18

标签: android

我有一个问题我无法解决qal。 我已经尝试过所有方法。 这是AsyncTask中的一个问题,它不应该是正确的。

有人能以正确的方式告诉我。

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)

2 个答案:

答案 0 :(得分: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 i)。

希望它有所帮助。 Saludos

答案 1 :(得分:0)

看起来你的img从未被初始化,至少在发布的代码中。如果是这样,getCount()正确地为空。

如果这是真的,你需要这样的东西:

private int[] img = someIntArrayWithYourIds;

修改:澄清:

尝试在getCount中设置此行中的调试器断点,并查看img和img.length的值:

        return this.img.length;

很可能,img在那里是空的。