Fragment 中的 this 关键字在

this keyword in Fragment is not getting implemented while

我创建了两个 Fragment,目标 Fragment 有一个可扩展列表视图,它也有一个适配器 class。当我试图为三个参数之一的上下文传递此关键字时,它不起作用。谁能指导我哪里出错了。

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //Initializations
    expandableListView=view.findViewById(R.id.expand_list);

    content_collection();

    //listadapter
    listAdapter=new ExListAdapter(this, branches,branchntopic);

    expandableListView.setAdapter(listAdapter);
}

this 不是片段中的有效上下文,您需要使用 requireContext()getContext() 代替。

片段 class 不从上下文 class 扩展,因此片段不是上下文。但是 Activity/AppCompatActivity 扩展自 Context class.

使用 getActivity() 代替 this。 这应该可以解决问题,因为该列表不在片段上下文中,而是在调用片段

的 activity 中

您可以使用 getActivity()getContext() 而不是使用它。 由于您使用的是片段,因此您无法将 this 关键字作为上下文传递。