Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

java - Is not accessible in current context

I have the following code

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
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Creation Dead Lock

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.

The way to solve it is put the ModelViewHolder class into a new *.java file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...