Android XML dataHandler getResources()未定义

时间:2014-01-23 09:12:18

标签: java android xml arrays datahandler

我是Java的新手,我正在尝试为xml创建一个dataHandler。 但是我收到一个错误:“方法getResources()未定义类型CasusHandler”。

我忘了什么?

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 个答案:

答案 0 :(得分:1)

getResources()Context的一种方法。在这里,您可以将Context引用传递给方法setCasusArray()

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

答案 1 :(得分:0)

您需要将Context传递给您的类,以便调用getResources

您应该做的是以下内容:

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

}

希望有所帮助