我有一个Activity,其布局包含一个带有FrameLayout + ListView的DrawerLayout(来自支持的库)。 在我的FrameLayout中,我有一个ListFragment,看起来像这样(aprox):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_post,
container, false);
mListHeader = inflater.inflate(R.layout.header,
container, false);
mListFooter = inflater.inflate(R.layout.footer,
container, false);
// inflate xml layout into Views
linkUI();
// set Views actions
setAction();
return fragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// add header and footer before settings adapter (to work in
// pre-KITKAT versions)
getListView().addHeaderView(mListHeader);
getListView().addFooterView(mListFooter);
if (listAdapter == null) {
listAdapter = new MyAdapter(getActivity());
}
setListAdapter(listAdapter);
pbListLoading.setVisibility(View.VISIBLE);
}
@Override
public void onDestroy() {
setListAdapter(null);
super.onDestroy();
}
问题是,来自onActivityCreated的setListAdapter(listAdapter)行似乎存在问题,它会引发此异常:
01-24 18:20:55.336: E/AndroidRuntime(20789): FATAL EXCEPTION: main
01-24 18:20:55.336: E/AndroidRuntime(20789): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.widget.ListView.clearRecycledState(ListView.java:519)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.widget.ListView.resetList(ListView.java:505)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.widget.ListView.setAdapter(ListView.java:448)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.ListFragment.setListAdapter(ListFragment.java:182)
01-24 18:20:55.336: E/AndroidRuntime(20789): at com.example.PostFragment.onActivityCreated(PostFragment.java:138)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.os.Handler.handleCallback(Handler.java:615)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.os.Looper.loop(Looper.java:153)
01-24 18:20:55.336: E/AndroidRuntime(20789): at android.app.ActivityThread.main(ActivityThread.java:5006)
01-24 18:20:55.336: E/AndroidRuntime(20789): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 18:20:55.336: E/AndroidRuntime(20789): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 18:20:55.336: E/AndroidRuntime(20789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-24 18:20:55.336: E/AndroidRuntime(20789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-24 18:20:55.336:E / AndroidRuntime(20789):at dalvik.system.NativeStart.main(Native Method)
这段代码有什么问题?
答案 0 :(得分:1)
尝试将listview作为参数而不是片段的容器传递:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mListHeader = inflater.inflate(R.layout.header, view, false);
mListFooter = inflater.inflate(R.layout.footer, view, false);
}
并移除整个onCreateView
方法,ListFragment
已调用inflater.inflate(com.android.internal.R.layout.list_content, container, false);
如果您仍然遇到相同的错误,那么我认为您的MyAdapter可能存在问题。
希望这会对你有所帮助:)。
答案 1 :(得分:1)
使用页眉和页脚的代码:
mListHeader = inflater.inflate(R.layout.header, null);
mListFooter = inflater.inflate(R.layout.footer, null);
如果您使用当前代码,夸大的观看次数mListHeader
和mListFooter
将LayoutParams
作为FrameLayout.LayoutParams
的实例(您将container
作为第二个参数,它基本上建议膨胀的视图将具有的父类型)。但是,当您致电setListAdapter()
时,ListView
会做一些工作,它会迭代上面的视图并访问他们的LayoutParams
(ListView
期望成为一个实例导致AbsListView.LayoutParams
的{{1}}。{/ p>