我将FooterView添加到ListView时发生ClassCastException [英] ClassCastException when I add FooterView to ListView

查看:65
本文介绍了我将FooterView添加到ListView时发生ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段试图显示ListView.当我到达ListView的底部时,将在网络请求中检索到ListView的更多项目.为此,我想设置带有进度条的页脚.但是,当我滚动到列表的底部并且应该显示页脚时,我得到ClassCastException,并且应用程序崩溃.这是片段的onCreateView的相关部分:

I have a fragment in which I am trying to display a ListView. When I get to the bottom of the ListView, some more items for ListView will be retrieved in network request. For this I want to set a Footer with a progressbar. But when I scroll to the bottom of the List and it is supposed to show the Footer I get ClassCastException and the app crashes. Here is relevant part of onCreateView of the Fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_search_result, container, false);
        mContext=getActivity().getApplicationContext();


        mResultListView=(ListView)rootView.findViewById(R.id.list_searchresults);
        mResultListView.setOnScrollListener(new ResultListScrollListener());

        footerView = inflater.inflate(R.layout.endless_list_footer, null, false);
        mResultListView.addFooterView(footerView, null , false);

        mAdapter=new SearchResultListAdapter(mContext, mResultList, getFragmentManager());
        mResultListView.setAdapter(mAdapter);
        mResultListView.setOnItemClickListener(new ResultListListener());
        return rootView;
    }

这是endless_list_footer布局的xml:

Here is the xml of the endless_list_footer layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@drawable/search_item_bg" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />


</RelativeLayout>

该应用最初运行,但是当您向下滚动到页脚应位于的位置时,应用崩溃,这是logcat:

The app runs initially but when you scroll down to where footer should be, app crashs, here is logcat:

07-09 13:37:49.533: E/AndroidRuntime(6451): FATAL EXCEPTION: main
07-09 13:37:49.533: E/AndroidRuntime(6451): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.ListView.setupChild(ListView.java:2038)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.ListView.makeAndAddView(ListView.java:2007)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.ListView.fillDown(ListView.java:856)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.ListView.fillGap(ListView.java:820)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4636)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3899)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.os.Handler.handleCallback(Handler.java:608)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.os.Looper.loop(Looper.java:156)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at android.app.ActivityThread.main(ActivityThread.java:4987)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 13:37:49.533: E/AndroidRuntime(6451):     at dalvik.system.NativeStart.main(Native Method)

我已经看过各种答案,在尝试获取footerview时,我尝试将listview传递给充气机,这无济于事,我尝试了传递null,这是行不通的.我也尝试删除页脚布局中进度栏周围的相对布局,仍然是相同的错误.另外,我还要确保在设置适配器之前先添加页脚.我已经尝试实现了我能找到的尽可能多的解决方案,但是所有解决方案仍然给出相同的错误.

I have looked at various answers, I have tried passing listview to inflater when getting the footerview, it doesn't help, I've tried with passing null, doesn't work. Also I tried removing the relative layout around the progressbar in the footer layout, still same error. Also I've made sure that I am adding footer before setting adapter. I've tried to implement as many solutions I could find but all give the same error still.

编辑

我尝试通过以下方式显式设置footerView的LayoutParams:

I tried explicitly setting the LayoutParams of footerView with:

 footerView = inflater.inflate(R.layout.endless_list_footer, mResultListView, false);
 footerView.setLayoutParams(new android.widget.AbsListView.LayoutParams(android.widget.AbsListView.LayoutParams.MATCH_PARENT, android.widget.AbsListView.LayoutParams.WRAP_CONTENT));

我仍然收到无法将其从Viewgroup $ LayoutParams转换为AbsListView $ LayoutPArams的错误.这怎么可能??

I still get the error that it cannot convert from Viewgroup$LayoutParams to AbsListView$LayoutPArams. How is this possible??

推荐答案

在第二个参数中传递listview而不是null

In second argument pass listview instead of null

    footerView = inflater.inflate(R.layout.endless_list_footer, mResultListView, false);

第二个参数是此新膨胀视图的父级,通过传递列表视图,Layout参数将自动设置为该视图.

The 2nd argument is the parent of this newly inflated view and by passing the list view Layout params will get automatically set to the view.

这篇关于我将FooterView添加到ListView时发生ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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