如何修复imageview ClassCastException的布局参数?

时间:2011-10-02 03:36:31

标签: android classcastexception

这是我每次尝试为图像视图设置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的布局我试图设置layoutParam的..

<?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>

这是代码我膨胀视图,然后尝试设置Param但我一直收到上面的错误。

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

我不知道为什么我会继续这样做。一切似乎都是对的?我怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

您将imageView放在LinearLayout中。因此,您应该像这样修改布局参数代码

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