ClassCastException(图库+ImageView)
我已经按照教程创建了一个下面有图像视图的画廊。我想做的就是将图库放在底部,将图像视图放在上面,但是当我在 XML 中切换它们时,它给了我一个 ClassCastException。
这是 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是非工作版本,但将画廊放在 imageview 之上是可行的。
来自日志:
01-26 22:20:13.673: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-26 22:20:13.673: E/AndroidRuntime(349): at com.appinfluence.fanapp.v1.Photos.onCreate(Photos.java:36)
onCreate:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(Photos.this));
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(mImageIds[0]);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(mImageIds[position]);
}
});
}
其他有用的代码:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
I have followed a tutorial to create a gallery with an imageview below it. All I want to do is place the gallery on the bottom and the imageview above it, but when I switch them around in the XML it gives me a ClassCastException.
Here is the XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
That is the non-working version, but placing gallery above imageview works.
From log:
01-26 22:20:13.673: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-26 22:20:13.673: E/AndroidRuntime(349): at com.appinfluence.fanapp.v1.Photos.onCreate(Photos.java:36)
onCreate:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(Photos.this));
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(mImageIds[0]);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(mImageIds[position]);
}
});
}
Other helpful code:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嘿 NickNelson 现在我已经找到了您问题的解决方案,只需清理项目即可。
示例屏幕截图
Hey NickNelson Just now i got the solution to your problem, just clean the project it will work.
Sample screenshot
无需在布局中使用单独的
ImageView
,Gallery将显示图像。删除ImageView
并检查它。编辑
只需清理项目即可。 (正如尤干达尔所说。)
No need to use separate
ImageView
in the layout, Gallery will display the images. Remove theImageView
and check it..edit
just clean the project . ( as yugandhar said.)
请尝试在布局部分使用此 XML:
Kindly try this XML for the layout section: