programing

ListView.addHeaderView ()를 호출 할 때 ClassCastException이 발생합니까?

goodcopy 2021. 1. 14. 23:13
반응형

ListView.addHeaderView ()를 호출 할 때 ClassCastException이 발생합니까?


목록보기 위에 배치하려는 상당히 복잡한 레이아웃 (RelativeLayouts, TextViews 및 ImageViews 포함)이 있습니다. 이보기는 목록보기와 함께 스크롤되어야합니다.

이 코드를 사용하여 레이아웃을 목록보기에 헤더로 추가해 보았습니다.

View v = inflater.inflate(R.layout.list_view, container, false);

View header = inflater.inflate(R.layout.header_layout, container, false);

// populate views in the header

mList = (ListView)v.findViewById(R.id.list);
mList.addHeaderView(header);
mAdapter = new ReviewsAdapter(getActivity());
mList.setAdapter(mAdapter); <-- error occurs here

ReviewsAdapter확장하는 사용자 지정 어댑터 BaseAdapter입니다.

코드를 실행하면이 오류가 발생합니다.

11-25 17:19:14.802: E/AndroidRuntime(1215): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
11-25 17:19:14.802: E/AndroidRuntime(1215):     at android.widget.ListView.clearRecycledState(ListView.java:513)
11-25 17:19:14.802: E/AndroidRuntime(1215):     at android.widget.ListView.resetList(ListView.java:499)
11-25 17:19:14.802: E/AndroidRuntime(1215):     at android.widget.ListView.setAdapter(ListView.java:442)
11-25 17:19:14.802: E/AndroidRuntime(1215):     at com.coppi.storefront.product.ProductReviewsFragment.onCreateView(ProductReviewsFragment.java:104)

mList.addHeaderView(header)줄을 주석 처리 하면 오류가 발생하지 않습니다. 또한 문제없이 목록보기없이 헤더 레이아웃을 표시 할 수 있습니다.

나는 이것이 헤더 레이아웃의 내용과 관련이 있다고 가정하고 있지만 그 원인이 정확히 무엇인지 확실하지 않습니다.

다음은 헤더 xml입니다.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:id="@+id/header_section"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_sides"
            android:background="@color/pdp_availability_section_background" >

            <TextView
                android:id="@+id/header_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_sides"
                android:paddingBottom="@dimen/margin_sides"
                android:text="@string/ratings_reviews"
                android:textColor="#000"
                android:textSize="18dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/body_section"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/header_section" >

            <TextView
                android:id="@+id/product_title"
                style="@style/ProductTitleFont"
                android:layout_marginBottom="@dimen/product_title_bottom_margin"
                android:layout_marginLeft="@dimen/margin_sides"
                android:layout_marginRight="@dimen/margin_sides" />

            <RelativeLayout
                android:id="@+id/attributes_section"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/product_title"
                android:layout_centerHorizontal="true"
                android:layout_margin="@dimen/margin_sides" >

                <LinearLayout
                    android:id="@+id/overall_section"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="bottom" >

                    <TextView
                        android:id="@+id/overall_label"
                        style="@style/ProductTitleFont"
                        android:layout_width="wrap_content"
                        android:text="@string/overall_rating" />

                    <ImageView
                        android:id="@+id/overall_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:src="@drawable/icon_rating_empty" />

                    <ImageView
                        android:id="@+id/overall_2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/icon_rating_empty" />

                    <ImageView
                        android:id="@+id/overall_3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/icon_rating_empty" />

                    <ImageView
                        android:id="@+id/overall_4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/icon_rating_empty" />

                    <ImageView
                        android:id="@+id/overall_5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/icon_rating_empty" />

                    <TextView
                        android:id="@+id/overall_score"
                        style="@style/ProductTitleFont"
                        android:layout_width="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="4.6" />
                </LinearLayout>

                <Button
                    android:id="@+id/rate_review_button"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/overall_section"
                    android:layout_marginBottom="@dimen/margin_sides"
                    android:layout_marginTop="@dimen/margin_sides"
                    android:text="@string/rate_review_button_text" />
            </RelativeLayout>

            <View
                android:id="@+id/attributes_divider"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_below="@id/attributes_section"
                android:layout_marginBottom="@dimen/margin_sides"
                android:layout_marginTop="@dimen/margin_sides"
                android:background="@color/pdp_section_divider" />

            <TextView
                android:id="@+id/review_count"
                style="@style/ProductTitleFont"
                android:layout_width="wrap_content"
                android:layout_below="@id/attributes_divider"
                android:layout_marginLeft="@dimen/margin_sides"
                android:text="0 " />

            <TextView
                style="@style/ProductTitleFont"
                android:layout_width="wrap_content"
                android:layout_alignBaseline="@id/review_count"
                android:layout_marginRight="@dimen/margin_sides"
                android:layout_toRightOf="@id/review_count"
                android:text="@string/customer_reviews" />

            <View
                android:id="@+id/review_count_divider"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_below="@id/review_count"
                android:layout_marginBottom="@dimen/margin_sides"
                android:layout_marginTop="@dimen/margin_sides"
                android:background="@color/pdp_section_divider" />
        </RelativeLayout>
    </RelativeLayout>

Update: I tried reducing the header .xml file to just a single TextView and the problem continues. So I don't believe the problem is being cause by something in the xml.


FrameLayout and AbsListView convert their children layout params to FrameLayout.LayoutParams and AbsListView.LayoutParams. This is where the casting fails.

View header = View.inflate(this, R.layout.header_layout, null);

should fix it.

Edit: As mentioned in the comments, changing the ViewGroup Parameter of the inflate call also makes it work:

header = inflater.inflate(R.layout.header_layout, null, false);

Although Souvlaki's method will fix the issue and allow you to continue working. It is always best to give the view being inflated a reference to the container it will be in. This allows for Android to correctly inflate it for the right context.

What you should do is find the ListView and then pass that to the inflate.

ListView listView = (ListView) layout.findViewById(R.id.listview);
View header = inflater.inflate(this, R.layout.header_layout, listView, false);

I see same situation. in my case, it's a trouble that call timing for ListView.addHeaderView().

Before: (Error)
ListView.addHeaderView() is called in Fragment.onCreateView() method.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_main, container, false);
    mListView = (ListView) layout.findViewById(R.id.listview);
    mListView.addHeaderView(inflater.inflate(R.layout.list_header_book, listview, false));
    return layout;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mAdapter = new BookAdapter(getActivity(), mBookList);
    mListView.setOnItemClickListener(mBookItemClickListener);
    mListView.setAdapter(mAdapter);
}


After:(OK)
ListView.addHeaderView() is called in Fragment.onActivityCreated() method.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mAdapter = new BookAdapter(getActivity(), mBookList);
    mListView = (ListView) getView().findViewById(R.id.listview);
    mListView.setOnItemClickListener(mBookItemClickListener);
    mListView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.list_header_book, null));
    mListView.setAdapter(mAdapter);
}

From drspaceboo's post above, this worked for me:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    mListLayoutView = inflater.inflate(getLayoutId(), container, false);

    int headerLayoutId = R.layout.list_header_layout;
    mListHeaderView = inflater.inflate(headerLayoutId, mResourcesListView, false);

    // ...

    mResourcesListView.addHeaderView(mListHeaderView, null, false);

Where getLayoutId() returns the main layout that contains the ListView xml

ReferenceURL : https://stackoverflow.com/questions/8275669/classcastexception-when-calling-listview-addheaderview

반응형