-1

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)
4

2 に答える 2

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

于 2012-11-06T12:37:38.210 に答える
0

少なくとも投稿されたコードでは、imgが初期化されていないようです。その場合、getCount()は正しくnullを取得します。

それが本当なら、あなたは次のようなものが必要になります:

private int[] img = someIntArrayWithYourIds;

編集:説明:

getCountのこの行にデバッガブレークポイントを設定してみて、imgとimg.lengthの値を確認してください。

        return this.img.length;

おそらく、imgはnullです。

于 2012-11-06T13:19:46.923 に答える