cannot reference "this" before supertype constructor has been called

3.6k Views Asked by At

I've been having trouble with some code lately:

public class SightsAdapter extends ArrayAdapter<Sights> {

    static int mColorResourceId;

    public SightsAdapter(ArrayList<Integer> sights, int colorResourceId) {
        super(this, 0, sights);
        mColorResourceId=colorResourceId;
    }
}

I already tried using the static method in the Sights java class, but I still get the this underlined and an error message saying

cannot reference "this" before supertype constructor has been called

PS I already reviewed the other questions of this type and they didn't really help me . I'm a complete newbie here so if someone could explain to me what I'm doing wrong will be greatly appreciated.

1

There are 1 best solutions below

2
Honza Zidek On

It looks like you are confused as what to pass to the super constructor (belonging to ArrayAdapter) as the first parameter Context.

For example here you have an example, the main part is that:

public class SightsAdapter extends ArrayAdapter<Sights> {

    public SightsAdapter(Context context, ArrayList<Integer> sights, int colorResourceId) {
        super(context, 0, sights);
        ...
    }
}

Context is something which must come from outside, your SightsAdapter must receive it as a parameter. That's the concept of context in applications: it is something which is injected from the environment in which the application/objects is running at the moment.