getContext() 无法识别

getContext() not recognised

getContext() 它显示 "Non-static method 'getContext()' cannot be referenced from a static context" 如何解决它,或者我应该叫什么来代替它。

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder > {

private List<CategoryModel> CategoryModelList;

public CategoryAdapter(List<CategoryModel> categoryModelList) {
    CategoryModelList = categoryModelList;
}

@NonNull
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View view = LayoutInflater.from(ViewGroup.getContext()).Inflate(R.layout.category_item,viewGroup,false);
    return null;
}

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 0;
}

您正在使用 ViewGroup 但它应该是 viewGroup

ViewGroup.getContext()

试试:

viewGroup.getContext()

试试这个:

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder > {
private Context context;

private List<CategoryModel> CategoryModelList;

public CategoryAdapter(List<CategoryModel> categoryModelList, Context Mcontext) {
    CategoryModelList = categoryModelList;
    this.context = Mcontext;
}

@NonNull
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View view = LayoutInflater.from(context).Inflate(R.layout.category_item,viewGroup,false);
    return null;
}

@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 0;
}