Question

I have an Activity which layout holds a DrawerLayout (from supported library) with a FrameLayout + ListView. In my FrameLayout I have a ListFragment, which looks like this (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();
}

Problem is, there seems to be a problem at: setListAdapter(listAdapter) line from onActivityCreated, that throws this exception:

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)

What could be wrong with this code?

Was it helpful?

Solution

For the code of the header and footer use:

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

If you use your current code the inflated views mListHeader and mListFooter will have as LayoutParams an instance of FrameLayout.LayoutParams(you pass the container as the second parameter which basically suggest the type of the parent that the inflated views will have). However when you call setListAdapter() the ListView does some work under the hood where it will iterate over the views above and access their LayoutParams(which the ListView expects to be an instance of AbsListView.LayoutParams) resulting in the ClassCastException.

OTHER TIPS

Try to pass the listview as parameter instead of the fragment's container:

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

And remove your entire onCreateView method as ListFragment already calls inflater.inflate(com.android.internal.R.layout.list_content, container, false);

If you still have the same error, then I think you might have a problem in your MyAdapter.

Hope this will help you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top