android中的网格视图问题

时间:2011-05-28 05:11:51

标签: android

在我的应用程序中,我使用网格视图进行图像显示。在应用程序中我从网址获取图像并在网格视图中显示。我的日志cat错误是:java.lang.ClassCastException: android.widget.Gallery$LayoutParams

我的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 float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
        return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
    }
}    
}

我也在manifest xml中提供了互联网许可。

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)

请帮帮我。

2 个答案:

答案 0 :(得分:0)

我认为Android的JPG图像效果不佳,尝试使用PNG并告诉我们

答案 1 :(得分:0)

尝试

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

而不是

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