Pandarian Ld Pandarian Ld - 1 month ago 13
Android Question

ClassCastException on LinearLayout.measure when measure ListView height

In my Android application, there is a layout which contains two ListView. The layout is implemented as the following

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/scroll_child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/qranking_question"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<com.mobeta.android.dslv.DragSortListView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
dslv:collapsed_height="2dp"
dslv:drag_enabled="true"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onLongPress"
dslv:float_alpha="0.5"
dslv:max_drag_scroll_speed="0.5"
dslv:track_drag_sort="false" />

<View
android:layout_width="@dimen/seperator_view_height"
android:layout_height="match_parent" />

<ListView
android:id="@+id/qranking_choice"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>
</LinearLayout>

</ScrollView>


And for each item in the ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/qranking_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/qranking_item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />

</LinearLayout>


As you can see, there are two views in ListView's item. One is a TextView and another one is a ImageView. One of them will be shown at a time, either TextView only or ImageView only.
There is no error when I show only TextView, but when I show only ImageView (by set visible="GONE" on TextView) I got an error when at the line

view.measure(...);


of the following method

private float getListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return 0.0f;

float screenWidth = DeviceProperties.getInstance().getScreenWidthDP(context); // DeviceProperties.getInstance().getScreenWidth();
float seperatorWidth = context.getResources().getDimension(R.dimen.seperator_view_height);
float viewHorizontalMargin = context.getResources().getDimension(R.dimen.activity_horizontal_margin);
final int FAKE_MARGIN = 2;
int desiredWidth = (int) ((screenWidth - seperatorWidth - viewHorizontalMargin) / 2) - (2 * FAKE_MARGIN);
int totalHeight = 0;
View view = null;

for (int i = 0; i < listAdapter.getCount(); i++) {
QRankingItem item = (QRankingItem) listAdapter.getItem(i);
String text = item.getDescription();

view = listAdapter.getView(i, view, listView);

if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
ViewGroup.LayoutParams.MATCH_PARENT));

view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
float measuredHeight = view.getMeasuredHeight();
...
}

return totalHeight;
}


The error said

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams


For more details, I use a custom ListView's Adapter which return a view as the following

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = null;

if (view == null) {
view = ((LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listitem_qranking, null);

QRankingItem item = mModelList.get(i);
TextView descView = (TextView) view.findViewById(R.id.qranking_item_name);
ImageView imgView = (ImageView) view.findViewById(R.id.qranking_item_img);
float screenWidth = DeviceProperties.getInstance().getScreenWidth();
float imgWidth = (screenWidth / 2) - (0.2f * screenWidth);
imgView.setLayoutParams(new ViewGroup.LayoutParams((int) imgWidth, (int) imgWidth));

if (item.getType() == TYPE_RANKING.IMAGE) {
descView.setVisibility(View.GONE);
imgView.setImageBitmap(item.getChoiceImg());
} else {
descView.setText(item.getDescription());
imgView.setVisibility(View.GONE);
}
}

return view;
}


How to fix this error?
Thank you in advance!!

Answer

Plese, see your logcat. You sholud use

imgView.setLayoutParams(new LinearLayout.LayoutParams((int) imgWidth, (int) imgWidth));

instead of

imgView.setLayoutParams(new ViewGroup.LayoutParams((int) imgWidth, (int) imgWidth))