0

我是安卓新手。。

我在使用图像 url 的网格布局中遇到问题..

我的代码..

public class act extends Activity {
    static String uri1 = "https://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
    static String uri2 = "https://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
    static String uri3 = "https://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
    public static String[] urls = { uri1, uri2, uri3 };
    public Bitmap bitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView grd = (GridView) findViewById(R.id.gridView1);
        grd.setAdapter(new ImageAdapter(this));
        grd.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                    long id) {
                Toast.makeText(getBaseContext(), "pic" + (pos + 1) + "select ",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;
        private int itemBackground;

        ImageAdapter(Context c) {
            context = c;
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return urls.length;
        }

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

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

        public View getView(int pos, View cv, ViewGroup vg) {
            Bitmap bitmap = DownloadImage(urls[pos]);
            ImageView imageview = new ImageView(context);
            imageview.setImageBitmap(bitmap);
            return cv;
        }

        private Bitmap DownloadImage(String URL) {
            final String URL1 = URL;
            new Thread() {
                public void run() {
                    InputStream in = null;
                    Message msg = Message.obtain();
                    msg.what = 1;
                    try {
                        in = OpenHttpConnection(URL1);
                        Bitmap bitmap = BitmapFactory.decodeStream(in);
                        Bundle b = new Bundle();
                        b.putParcelable("bitmap", bitmap);
                        msg.setData(b);
                        in.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }.start();
            return bitmap;
        }

        private InputStream OpenHttpConnection(String urlString)
                throws IOException {
            InputStream in = null;
            int response = -1;
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
            if (!(conn instanceof HttpURLConnection))
                throw new IOException("Not an HTTP connection");
            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();
                }
            } catch (Exception ex) {
                throw new IOException("Error connecting");
            }
            return in;
        }
    }
}

日志猫

       E/AndroidRuntime(832):   at 
   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
   E/AndroidRuntime(832):   at 
   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
    E/AndroidRuntime(832):  at 
    android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
     E/AndroidRuntime(832):     at 
    android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
     E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
     E/AndroidRuntime(832):     at 
     android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
      E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
     E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
     E/AndroidRuntime(832):     at 
      android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
      E/AndroidRuntime(832):    at 
      android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
      E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
      E/AndroidRuntime(832):    at 
      android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
      E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
      E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
      E/AndroidRuntime(832):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
      E/AndroidRuntime(832):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
      E/AndroidRuntime(832):    at android.os.Handler.dispatchMessage(Handler.java:99)
       E/AndroidRuntime(832):   at android.os.Looper.loop(Looper.java:123)
       E/AndroidRuntime(832):   at android.app.ActivityThread.main(ActivityThread.java:3683)
       E/AndroidRuntime(832):   at java.lang.reflect.Method.invokeNative(Native Method)
       E/AndroidRuntime(832):   at java.lang.reflect.Method.invoke(Method.java:507)
       E/AndroidRuntime(832):   at 
       com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        E/AndroidRuntime(832):  at 
       com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        E/AndroidRuntime(832):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

我建议您阅读:http: //xjaphx.wordpress.com/2011/06/12/custom-grid-view-of-applications/

于 2013-04-29T10:41:08.637 回答