BtAndro
BtAndro

Reputation: 71

Error to replace fragment by Listfragment

I did this

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment obj = null;
    ListFragment listfragment = null;
    switch (position) {
        case 0:
            listfragment= new F1_fr();
            break;
        case 1:
            obj= new F6_fr();
            break;
        case 2:
            obj= new F3_fr();
            break;
        case 3:
            obj= new F4_fr();
            break;
        case 4:
            obj= new F5_fr();
            break;
    }

    if (obj != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.container, obj)
            .commit();
    }
    else if (listfragment != null) {
        // do stuff if its a listfragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.container, listfragment)
            .commit();
    }
}

But i have an error here : .replace(R.id.container, listfragment) that said me replace android.supportv4.app.fragment in fragment transaction cannot be applied to(int, android.app.ListFragment)

    public class F1_fr extends ListFragment {
   View rootview;
    TextView t;
    ListView listView;

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.f1_lay,container,false);
        listView =(ListView) rootview.findViewById(R.id.list);
        rootview.findViewById(R.id.semi_transparent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), qr.class);
                ;

                startActivity(intent);


            }
        });



        return rootview;

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final String[] items = getResources().getStringArray(R.array.heroes);
        final ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);

        setListAdapter(aa);

    }

}

Upvotes: 1

Views: 1514

Answers (2)

Tor_Gash
Tor_Gash

Reputation: 164

When you automatically optimize your imports (like Ctrl-Shift-O in Eclipse), the system should give you a choise what Fragment to import. Choose a support version or a ListFragment like laalto said.

Upvotes: 2

laalto
laalto

Reputation: 152807

You're mixing android.app fragments with support library fragments.

Since your intention seems to be using support library fragments, replace your android.app fragment imports such as android.app.ListFragment with their support library counterparts such as android.support.v4.app.ListFragment.

Upvotes: 1

Related Questions