Search code examples
androidlistviewandroid-listfragment

Exception in ListFragment: The specified child already has a parent


I'm making a downloader app, and i got child's parents error after the onCreateView return section. I tried a lots of things, but nothing help. I tried: ((ViewGroup)dlistView.getParent()).removeView(dlistView); after that the compiler says:

01-12 12:05:15.558: E/AndroidRuntime(10740): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

or remove the container/ i got an npe/ or remove the V but the V.getParent() is null.

Which view is the parent, and which is the child?

The code:

public static class DownloadingFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
        public DownloadingFragment() {
        }

        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View V = inflater.inflate (R.layout.list, null);    
            ListView dlistView = (ListView) V.findViewById(R.id.dlist);
            List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
            downloadInfo.add(new DownloadInfo("File", 1000));
            DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
                    getActivity().getApplication(),R.layout.list, downloadInfo);
            dlistView.setAdapter(diaa);
        return dlistView; // throw exception 
        }
    }
//...
}

Thank you in advance for your help.

EDIT: the new code:

public static class DownloadingFragment extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";
        public DownloadingFragment() {
        }
        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View V = inflater.inflate (R.layout.list, null);    
        return V;
        }

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            ListView dlistView = (ListView) getView().findViewById(R.id.dlist);
            List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
            downloadInfo.add(new DownloadInfo("File", 1000));
            DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
                    getActivity().getApplication(),R.layout.list, downloadInfo);
            dlistView.setAdapter(diaa);
        }
    }
//...
}

Solution

  • that's because you return the listview which has a parent (somewhere in V's layout tree) .

    for any type of fragment , onCreateView should return a view that doesn't have any parent .

    in this case , since it's a listFragment , create the listview (either using xml or programmatically ) , and then return it . do not allow it to have any parents since it needs to have a parent as a fragment .

    read here for more information :

    The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.