问题描述
我有以下代码
public abstract class BaseAdapter<T, V extends BaseAdapter.ViewHolder> extends ArrayAdapter<T> {
public BaseAdapter(Context context, int resource, Collection<T> collection) {
// typical constructor logic
}
// some other custom defined methods
public static class ViewHolder {
// custom defined logic
}
}
public class ModelAdapter extends BaseAdapter<Model, ModelAdapter.ModelViewHolder> {
public ModelAdapter(Context context, int resource, Collection<Model> collection) {
super(context, resource, collection);
// typical constructor logic
}
public static class ModelViewHolder extends ViewHolder {
// custom defined logic
}
}
BaseAdapter和ModelAdapter位于不同的文件中。问题是我在尝试定义ModelAdapter时遇到编译错误:
在当前上下文中无法访问ModelViewHolder
The BaseAdapter and ModelAdapter are in separated files. The problem is that I have a compilation error when trying to define the ModelAdapter:ModelViewHolder is not accessible in current context
I我真的不明白这个错误,也无法弄清楚我做错了什么。有人可以向我解释这个问题或可能澄清这种情况的链接吗?
I don't really understand this error and can't figure out what I am doing wrong. Can somebody explain to me this problem or a link that may clarify this situation?
推荐答案
创建死锁
您使用 ModelAdapter.ModelViewHolder
作为 BaseAdapter的模板参数
,并让 ModelAdapter
extends BaseAdapter
,然后编译器尝试创建 ModelViewHolder
首先,但尚未创建 ModelAdapter.ModelViewHolder
(类型为 Class )的类。它必须等待 ModelAdapter
才能创建,因为 ModelViewHolder
在 ModelAdapter 。
You use ModelAdapter.ModelViewHolder
as the template parameter of BaseAdapter
, and let ModelAdapter
extends BaseAdapter
, then the compiler tried to create ModelViewHolder
first, but the class of ModelAdapter.ModelViewHolder
(the type is Class) is not yet created. It must wait for ModelAdapter
to be created, because ModelViewHolder
is in the scope of ModelAdapter
.
解决问题的方法是将 ModelViewHolder
类放入一个新的* .java文件中。
The way to solve it is put the ModelViewHolder
class into a new *.java file.
这篇关于在当前情况下无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!