开发者

grid view problem in android

in my app i am using grid view for image display. in app i am getting image from url and displaying in grid view. my log cat error is:java.lang.ClassCastException: android.widget.Gallery$LayoutParams

my main.xml:

<?xml version="1.0" encoding="utf-8"?>

<GridView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/gridview"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:numColumns="auto_fit"
  android:verticalSpacing="10dp"
  android:horizontalSpacing="10dp"
  android:columnWidth="90dp"
  android:stretchMode="columnWidth"
  android:gravity="center" >
</GridView>

main.java:

  public class main extends Activity {
    /** Called when the activity is first created. */
   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));       
}
public class ImageAdapter extends BaseAdapter {
    /** The parent context */
    private Context myContext;

    /** URL-Strings to some remote images. */
    private String[] myRemoteImages = {
      "http://xxxxxx/xxxx/xxx/original/1306493038_spot.jpg",
                    "http://xxxxxxxxxxxxx/xxxx/xxx/original/1306493281_spot.jpg",
                    "http://xxxxx/xxxx/xxx/original/1306500350_spot.jpg"
    };

    /** Simple Constructor saving the 'parent' context. */
    public ImageAdapter(Context c) { this.myContext = c; }

    /** Returns the amount of images we have defined. */
    public int getCount() { return this.myRemoteImages.length; }

    /* Use the array-Positions as unique IDs */
    public Object getItem(int position) { return position; }
    public long getItemId(int position) { return position; }

    /** Returns a new ImageView to
     * be displayed, depending on
     * the position passed. */
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.myContext);

        try {
                            /* Open a new URL and get the InputStream to load data from it. */
                            URL aURL = new URL(myRemoteImages[position]);
                            URLConnection conn = aURL.openConnection();
                            conn.connect();
                            InputStream is = conn.getInputStream();
                            /* Buffered is always good for a performance plus. */
                            BufferedInputStream bis = new BufferedInputStream(is);
                            /* Decode url-data to a bitmap. */
                            Bitmap bm = BitmapFactory.decodeStream(bis);
                            bis.close();
                            is.close();
                            /* Apply the Bitmap to the ImageView that will be returned. */
                            i.setImageBitmap(bm);
                    } catch (IOException e) {
                            i.setImageResource(R.drawable.icon);
                            Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                    }

        /* Image should be scaled as width/height are set. */
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        /* Set the Width/Height of the ImageView. */
        i.setLayoutParams(new Gallery.LayoutParams(150, 150));
        return i;
    }

    /** Returns the size (0.0f to 1.0f) of the views
     * depending on the 'offset' to the center. */
    public 开发者_如何学Gofloat getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
        return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
    }
}    
}

i give internet permission in manifest xml also.

log cat error:

     05-28 10:29:03.371: ERROR/AndroidRuntime(752): java.lang.ClassCastException: android.widget.Gallery$LayoutParams
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.widget.GridView.onMeasure(GridView.java:928)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.View.measure(View.java:7117)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2929)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.View.measure(View.java:7117)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.View.measure(View.java:7117)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2929)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
    05-28 10:29:03.371: ERROR/AndroidRuntime(752):     at android.view.View.measure(View.java:7117)

please help me.


I think android doesn't get well with JPG images, try with a PNG one and tell us


try

i.setLayoutParams(new GridView.LayoutParams(150, 150));

instead of

i.setLayoutParams(new Gallery.LayoutParams(150, 150));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜