Search code examples
javaandroidxmlarraysdatahandler

Android XML dataHandler getResources() is undefined


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

}

Solution

  • 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);
    }