开发者

How to fix LayoutParam's for imageview ClassCaseException?

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开发者_如何学Go_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?


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜