2

I am working on a Listfragment inside an activity. I have a nested class in an activity.

public static class DummySectionFragment extends ListFragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container);
        return contentView;
    }

but the compiler complain the DummySectionFragemt is not a fragment

    @Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.
    Fragment fragment = new DummySectionFragment(); // <-- complain not fragment
    // codes omitted .......
}

Complain from compiler: Type mismatch: cannot convert from MainActivity.DummySectionFragment to Fragment

When I make the DummySectionFragment directly extends Fragment, then it works, but I just do not understand why it do not work previously. Apparently, DummySectionFragment extends ListFragment which extends Fragment.It should be an implicit upcast here, I do not understand why it does not work:(

//make it directly extends Fragment -->
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View contentView=inflater.inflate(R.layout.activity_main_fragment_browse, container);
        return contentView;
    }
  • 2
    Make sure you're not using the support library's ListFragment (`android.support.v4.app.Fragment`) or if you are, that you're using the Fragment class from the support library as well. – dmon Dec 07 '12 at 21:37
  • thank you very much. I did not realise there are two packages with same classes. – user1886616 Dec 07 '12 at 21:57
  • @user1886616 Ya, I think this gets everyone. Google thought it would be a good idea to default that EVERY new app created with ADT 17(?) auto use the support library. – ahodder Dec 08 '12 at 00:11

1 Answers1

0
public static class DummySectionFragment extends ListFragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView;
        if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("1")){
            rootView = inflater.inflate(R.layout.fragment_tab1,container, false);               
        }
        else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("2")){
            rootView = inflater.inflate(R.layout.fragment_tab2,container, false);               
        }
        else if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("3")){
            rootView = inflater.inflate(R.layout.fragment_tab3,container, false);               
        }
        else{
            rootView = inflater.inflate(R.layout.fragment_tab,container, false);
        }
        return rootView;
    }
}

//make sure that you use import android.support.v4.app.ListFragment -->

lxg
  • 12,375
  • 12
  • 51
  • 73