如何修复 imageview ClassCaseException 的 LayoutParam?

发布于 2024-12-07 18:21:45 字数 1913 浏览 0 评论 0原文

这是我每次尝试为图像视图设置 LayoutParam 时遇到的错误。

10-02 03:27:53.081: ERROR/AndroidRuntime(1272): FATAL EXCEPTION: main
10-02 03:27:53.081: ERROR/AndroidRuntime(1272): java.lang.ClassCastException: android.widget.Gallery$LayoutParams
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:659)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.view.View.measure(View.java:8313)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.view.ViewGroup.measureChild(ViewGroup.java:3109)

这是我的 imageview 布局,我试图为其设置布局参数。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
  android:id="@+id/imageView"
  android:layout_width="50dip"
  android:layout_height="50dip" android:src="@drawable/stub"  android:scaleType="centerCrop"/>
<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1" android:layout_gravity="left|center_vertical"   android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>

这是我膨胀视图然后尝试设置参数的代码,但我不断收到上面的错误。

  public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.lazyitemt, null);


    ImageView image=(ImageView)vi.findViewById(R.id.imageView);

    image.setLayoutParams(new Gallery.LayoutParams(150, 150));
    imageLoader.DisplayImage(data[position], activity, image);
    return vi;
}

我不知道为什么我一直收到这个。一切似乎都是对的?我该如何解决这个问题?

This is the error i get each time i try and set the LayoutParam's for a image view.

10-02 03:27:53.081: ERROR/AndroidRuntime(1272): FATAL EXCEPTION: main
10-02 03:27:53.081: ERROR/AndroidRuntime(1272): java.lang.ClassCastException: android.widget.Gallery$LayoutParams
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:659)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.view.View.measure(View.java:8313)
10-02 03:27:53.081: ERROR/AndroidRuntime(1272):     at android.view.ViewGroup.measureChild(ViewGroup.java:3109)

Here is my layout with the imageview i am trying to set the layoutParam's for..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
  android:id="@+id/imageView"
  android:layout_width="50dip"
  android:layout_height="50dip" android:src="@drawable/stub"  android:scaleType="centerCrop"/>
<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1" android:layout_gravity="left|center_vertical"   android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>

And here is the code i inflate the view and then try to set the Param's but i keep getting the error above.

  public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.lazyitemt, null);


    ImageView image=(ImageView)vi.findViewById(R.id.imageView);

    image.setLayoutParams(new Gallery.LayoutParams(150, 150));
    imageLoader.DisplayImage(data[position], activity, image);
    return vi;
}

I dont know why i keep getting this. Everything seems to be right? How can i resolve this?

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

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

发布评论

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

评论(1

夜清冷一曲。 2024-12-14 18:21:45

您将 imageView 放入 LinearLayout 中。所以,你应该像这样修改你的布局参数代码

image.setLayoutParams(new LinearLayout.LayoutParams(150, 150));

You put imageView inside LinearLayout. So, you should modify your layout param code like this

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