Home » Android » java – Fragments Incompatible types

java – Fragments Incompatible types

Posted by: admin June 15, 2020 Leave a comment

Questions:

There Activity inherits ActoinBarActivity, it describes the sidebar (NavigationDrawer), by clicking on its elements open fragments. In one of the fragments have listView, by clicking on the item which I want to open another fragment ( staff- a list of employees – employee data). But I is an error

Incompatible types:
Required: Android.app.Fragment
Found:com.abc.app.EmployeeDetails

public class MyEmployeeFragment extends Fragment {
//some code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    userList = new ArrayList<User>();
    sAdapter = new CustomAdapter(getActivity(),userList);
    View rootView = inflater.inflate(R.layout.my_employe, container, false);
    ListView lv = (ListView)rootView.findViewById(R.id.list);
    lv.setAdapter(sAdapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
            Fragment f = new EmployeeDetails(); // ERROR
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.content_frame, f);
            ft.commit();
            Log.i("TAG", "itemClick: position = " + position + ", id = "
            + id);
        }
    });

EmployeeDetails

public class EmployeeDetails extends Fragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_employee_details, container, false);
    }


}

Answer is:

All fragments must import android.app.Fragment; not android.support.v4.app.Fragment;

How to&Answers:

In your class EmployeeDetails declaration, do this:

public class EmployeeDetails extends Fragment {
......

And use import android.app.Fragment;

not use import android.support.v4.app.Fragment;

Answer:

Use import android.support.v4.app.Fragment;
Its Working me.