ClassCastException异常调用ListView.addHeaderView时()? [英] ClassCastException when calling ListView.addHeaderView()?

查看:179
本文介绍了ClassCastException异常调用ListView.addHeaderView时()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的布局(含RelativeLayouts,TextViews和ImageViews),我希望把上面的列表视图。这种观点应该滚动与ListView。

I have a fairly complex layout (containing RelativeLayouts, TextViews and ImageViews) that I want to place above a listview. This view should scroll with the listview.

我试图用这个code添加布局到ListView一个标题:

I tried adding the layout as a header to the listview using this code:

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

当执行code,我得到这个错误:

Upon executing the code, I get this error:

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(头)行我没有得到这个错误。我还可以显示表头布局不,没有任何问题的列表视图。

If I comment out the mList.addHeaderView(header) line I do not get the error. I can also display the header layout without the listview with no problems.

我假设这已是与的头布局的内容,但我不知道究竟会是什么导致它。

I'm assuming this has something to do with the contents of the header layout but I'm not sure exactly what would be causing it.

下面是头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>

更新:我试图减少头.xml文件只是一个单一的TextView和问题仍然存在。所以,我不相信这个问题正在造成的东西在XML。

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 AbsListView 将他们的子女布局PARAMS到 FrameLayout.LayoutParams AbsListView.LayoutParams 。这是其中,铸失败。

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.

编辑: 正如前面提到的意见,改变充气调用的参数的ViewGroup也使得它的工作:

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

这篇关于ClassCastException异常调用ListView.addHeaderView时()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆