我有一个片段,我试图在其中显示ListView。当我到达ListView的底部时,将在网络请求中检索更多ListView项。为此,我想设置一个带有进度条的页脚。但是当我滚动到List的底部并且它应该显示Footer我得到ClassCastException并且应用程序崩溃。 这是片段onCreateView的相关部分:
@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:
<?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:
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)
我已经查看了各种答案,我在尝试获取页脚视图时尝试将listview传递给inflater,它没有帮助,我尝试过传递null,不起作用。此外,我尝试删除页脚布局中进度条周围的相对布局,仍然是相同的错误。此外,我确保在设置适配器之前添加页脚。我试图实现尽可能多的解决方案,但所有解决方案仍然存在相同的错误。
修改
我尝试使用:
显式设置footerView的LayoutParams 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的错误。这怎么可能?
答案 0 :(得分:0)
在第二个参数中传递listview而不是null
footerView = inflater.inflate(R.layout.endless_list_footer, mResultListView, false);
第二个参数是这个新膨胀视图的父级,通过传递列表视图,布局参数将自动设置为视图。