如何解决Fragment无法转换为Context错误?

时间:2018-06-16 13:03:14

标签: android android-fragments error-handling

在我的片段中,错误来自Android工作室。 错误:不兼容的类型:EmployeeSetaFragment无法转换为上下文

如何解决?

public class EmployeeSetFragment extends Fragment {

    private RecyclerView recyclerView;
    private PoemAdapter adapter;
    private ArrayList<Poem> employeeArrayList;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_people,container,false);

        employeeArrayList = new ArrayList<>();
        employeeArrayList.add(new Poem("no 1 ", "mark"));

recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

            adapter = new PoemAdapter(employeeArrayList);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(EmployeeSetFragment.this);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
            return  v;

1 个答案:

答案 0 :(得分:0)

使用此

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());

而不是

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(EmployeeSetFragment.this);