碎片不兼容的类型

时间:2014-06-06 14:06:36

标签: java android android-fragments

Activity继承ActoinBarActivity,它通过点击其元素打开的片段来描述侧栏(NavigationDrawer)。在其中一个片段中有listView,点击我要打开另一个片段的项目(员工 - 员工列表 - 员工数据)。但我是一个错误

  

不兼容的类型:   必需:Android.app.Fragment   实测值: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);
    }


}
  

答案是:

所有片段必须import android.app.Fragment;不是android.support.v4.app.Fragment;

2 个答案:

答案 0 :(得分:19)

在您的班级EmployeeDetails声明中,请执行以下操作:

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

并使用import android.app.Fragment;

不使用import android.support.v4.app.Fragment;

答案 1 :(得分:-3)

使用import android.support.v4.app.Fragment; 它工作我。