如何解决android中的网格视图问题
| 在我的应用程序中,我使用网格视图进行图像显示。在应用程序中,我从url获取图像并在网格视图中显示。我的日志猫错误是: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)));
}
}
}
我也在清单xml中提供了互联网许可。
记录猫错误:
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)
请帮我。
解决方法
我认为android不能很好地处理JPG图像,请尝试使用PNG并告诉我们
, 尝试
i.setLayoutParams(new GridView.LayoutParams(150,150));
代替
i.setLayoutParams(new Gallery.LayoutParams(150,150));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。