1

I am trying to convert string-array from strings.xml to array in Java class (the model part of MVC), there-for I cannot use getResources().getStringArray(R.array.array_name); (it works only in Android components such as activity, fragment and so on).

So I can use only Resources.getSystem().getStringArray(R.array.array_name); but When I try to run it in the emulator, I get an exception. I found similar questions that referred that problem here. but I didn't understand the solution. Here my exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danirg10000gmail.therpiststep1/com.danirg10000gmail.therpiststep1.MainActivity}: android.content.res.Resources$NotFoundException: String array resource ID #0x7f0b0000

(My exception is same as in the link above.)

In my code I have two classes one class represents questions, another class have a list of questions objects. Here is my code:

public class QuestionM {
private String mQuestion;
private String mAnswer;
private String mExplanation;

 //constructor
 public QuestionM(String question,String explanation) {
    mQuestion = question;
    mExplanation = explanation;
}

public class QuestionnaireM {
private List<QuestionM> mQuestionsList;

//constructor
public QuestionnaireM(){
    mQuestionsList = new ArrayList<>();
    Resources resources = Resources.getSystem();
//when i creating object of that class android points the crush here
    String [] questions = resources.getStringArray(R.array.test);
    String [] questionExplanations = resources.getStringArray(R.array.test_a);
    for (int i=0; i<questions.length; i++){
        QuestionM question = new QuestionM(questions[i],questionExplanations[i]);
        mQuestionsList.add(question);
    }

}

also I didn't quite understand the difference between system-level resources, and application-level resources, I search it in androidDevelopers and in Google but not found any good explanation. can somebody please explain that?

0

2 Answers 2

1

One suggestion, not sure if it will work. But you can try and let me know. Why not get the context in QuestionM constructor and initialize your class level context variable with the received context. Now use this context to

mContext.getResources().getStringArray(R.array.array_name);

public class QuestionM {

    private String mQuestion;
    private String mAnswer;
    private String mExplanation;
    private Context mContext;

    //constructor
    public QuestionM(String question,String explanation, Context context) {
        mQuestion = question;
        mExplanation = explanation;
        mContext = context;
    }

public class QuestionnaireM {

    private List<QuestionM> mQuestionsList;

    //constructor
    public QuestionnaireM(){
        mQuestionsList = new ArrayList<>();


    //when i creating object of that class android points the crush here
    String [] questions = mContext.getResources().getStringArray(R.array.test);
    String [] questionExplanations = mContext.getResources().getStringArray(R.array.test_a);
    for (int i=0; i<questions.length; i++){
        QuestionM question = new QuestionM(questions[i],questionExplanations[i]);
        mQuestionsList.add(question);
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

i tried it and it works. i past the Context only in the QuestionnaireM() constructor because only there i use the resources.** thanks**. but when you can really use the Resources.getSystem().getStringArray()? if their such method it means you can use it?
His way is just one level ahead of mine. here he used an instance of Context as a parameter to retrieve the resources.
1

According to the docs getSystem() does this:

Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).

Therefore calling getStringArray() with the resource Id R.array.test is totally useless since the id referenced is that of an Application resource.

If you want to load the contents of R.array.test, use getStringArray() from getResources().

You can pass a parameter of type Resources to the constructor or String[]. i.e. :

public QuestionnaireM(Resources resource) { 
   // stuffs
}

6 Comments

but i cannot use getResources() in java class.
@D.Rotenberg he is asking you to pass Resource object explicitly while creating the object of QuestionnaireM
Kyle Emmanuel - i tried similar approach suggested by @rahul that worked, so i guess yours solution will also work, although i didnt checked it. could you please give me more specific explanation about the system-level resources, and application-level resources?
@D.Rotenberg application level resources are those resources unique to the app, i.e. drawables, mipmaps, values, strings, etc.
so what are systemResources and when you use them?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.