Как исправить LayoutParam для изображения ViewClassException? - PullRequest
0 голосов
/ 02 октября 2011

Это ошибка, которую я получаю каждый раз, когда пытаюсь установить 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)

Вот мой макет с изображением, я пытаюсь установить 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>

А вот код, который я надуваю, а затем пытаюсь установить параметры, но я получаю ошибку выше.

  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 Ответ

2 голосов
/ 02 октября 2011

Вы помещаете imageView в LinearLayout.Итак, вы должны изменить свой код параметров макета следующим образом:

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