ClassCastException(图库+ImageView)

发布于 2024-12-29 09:27:11 字数 2096 浏览 3 评论 0原文

我已经按照教程创建了一个下面有图像视图的画廊。我想做的就是将图库放在底部,将图像视图放在上面,但是当我在 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;
    }   

http://4.bp.blogspot.com/_I2Ctfz7eew4/S_JxIfgKrfI/AAAAAAAAAzA/m1KUSIugojY /s1600/Gallery2.1.PNG

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;
    }   

http://4.bp.blogspot.com/_I2Ctfz7eew4/S_JxIfgKrfI/AAAAAAAAAzA/m1KUSIugojY/s1600/Gallery2.1.PNG

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2025-01-05 09:27:11

嘿 NickNelson 现在我已经找到了您问题的解决方案,只需清理项目即可。

示例屏幕截图

在此处输入图像描述

Hey NickNelson Just now i got the solution to your problem, just clean the project it will work.

Sample screenshot

enter image description here

等风来 2025-01-05 09:27:11

无需在布局中使用单独的ImageViewGallery将显示图像。删除ImageView并检查它。

编辑

只需清理项目即可。 (正如尤干达尔所说。)

No need to use separate ImageView in the layout, Gallery will display the images. Remove the ImageView and check it..

edit

just clean the project . ( as yugandhar said.)

居里长安 2025-01-05 09:27:11

请尝试在布局部分使用此 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical">

    <ImageView android:layout_alignParentTop="true"
       android:id="@+id/ImageView01" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/antartica1"/>

    <Gallery
        android:id="@+id/Gallery01" 
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

Kindly try this XML for the layout section:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical">

    <ImageView android:layout_alignParentTop="true"
       android:id="@+id/ImageView01" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/antartica1"/>

    <Gallery
        android:id="@+id/Gallery01" 
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文