在我下面的片段中,错误来自Android Studio。
错误:不兼容的类型: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;
最佳答案
用这个
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
代替
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(EmployeeSetFragment.this);
关于android - 如何解决无法将 fragment 转换为上下文错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50888166/