【问题标题】:Fragments Incompatible types片段不兼容的类型
【发布时间】:2014-06-06 14:06:36
【问题描述】:

还有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;

【问题讨论】:

  • @Cyber​​neticTwerkGuruOrc 这是我的代码
  • @Cyber​​neticTwerkGuruOrc 如何解决:HomeFragment 必须扩展 Fragment 没有效果
  • 你能告诉我们这门课EmployeeDetails吗?
  • @Cyber​​neticTwerkGuruOrc 你可以看到
  • 感谢您清楚地写出答案。也可以移到顶部。

标签: java android android-fragments


【解决方案1】:

在你的班级EmployeeDetails 声明中,这样做:

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

并使用import android.app.Fragment;

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

【讨论】:

  • 抱歉,我发帖的时候没看到。
  • 答案是:所有片段都必须导入android.app.Fragment; not android.support.v4.app.Fragment;
  • 这取决于他是否使用了支持库?
  • @Garf1eld 您实际上可以自己将其发布为答案。然后在允许的情况下返回检查。
  • 我的意思是@Garf1eld,因为他自己找到了解决方案。 ://
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多