问题描述
我有以下代码
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 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?
推荐答案
Creation Dead Lock
你使用ModelAdapter.ModelViewHolder
作为BaseAdapter
的模板参数,让ModelAdapter
扩展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.
这篇关于在当前上下文中不可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!