Android XML dataHandler getResources() is undefined

262 Views Asked by At

I'm relatively new to Java and I'm trying to create a dataHandler for an xml. But I get an error: "The method getResources() is undefined for the type CasusHandler".

What am I forgetting?

import android.content.res.Resources;

public class CasusHandler {

    public String[] casus;

    public void setCasusArray() {
        Resources res = getResources();
        this.casus = res.getStringArray(R.array.casus);
    }

    public String[] getCasusArray() {
        return this.casus;
    }

}
2

There are 2 best solutions below

0
On BEST ANSWER

getResources() is a method of Context. and here you can pass the Context reference to the method setCasusArray()

public void setCasusArray(Context context) {
   this.casus = context.getResources().getStringArray(R.array.casus);
}
0
On

you need to pass the Context to you class so you could call getResurces

what you should do is the following :

public class CasusHandler {

    public String[] casus;


    public void setCasusArray(Context context ) {
        Resources res = context.getResources();
        this.casus = res.getStringArray(R.array.casus);
    }

    public String[] getCasusArray() {
        return this.casus;
    }

}

Hope that helps