Java 片段不兼容的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24083788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fragments Incompatible types
提问by Garf1eld
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
有Activity
继承ActoinBarActivity
,它描述了侧边栏 ( NavigationDrawer
),通过点击它的元素打开片段。在其中一个片段中listView
,通过单击我想打开另一个片段的项目(员工 - 员工列表 - 员工数据)。但我是一个错误
Incompatible types: Required: Android.app.Fragment Found:com.abc.app.EmployeeDetails
不兼容的类型:必需: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
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;
所有片段import android.app.Fragment;
不得android.support.v4.app.Fragment;
采纳答案by Lucas Santos
In your class EmployeeDetails
declaration, do this:
在您的类EmployeeDetails
声明中,执行以下操作:
public class EmployeeDetails extends Fragment {
......
And use import android.app.Fragment;
并使用 import android.app.Fragment;
not use import android.support.v4.app.Fragment;
不使用 import android.support.v4.app.Fragment;
回答by user6427267
Use import android.support.v4.app.Fragment;
Its Working me.
使用import android.support.v4.app.Fragment;
它的工作我。