gpt4 book ai didi

android - ListFragment setListAdapter

转载 作者:行者123 更新时间:2023-11-29 21:16:20 26 4
gpt4 key购买 nike

我有一个 Activity,其布局包含一个 DrawerLayout(来自支持的库)和一个 FrameLayout + ListView。在我的 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): 在 dalvik.system.NativeStart.main(本地方法)

这段代码可能有什么问题?

最佳答案

对于页眉和页脚的代码使用:

mListHeader = inflater.inflate(R.layout.header, null);
mListFooter = inflater.inflate(R.layout.footer, null);

如果您使用当前代码,膨胀 View mListHeadermListFooter 将具有 LayoutParams FrameLayout.LayoutParams< 的实例(您将 container 作为第二个参数传递,这基本上暗示了膨胀 View 将具有的父级类型)。但是,当您调用 setListAdapter() 时,ListView 会在后台执行一些工作,它将遍历上面的 View 并访问它们的 LayoutParams( ListView 应该是 AbsListView.LayoutParams 的实例)导致 ClassCastException

关于android - ListFragment setListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com