XML : getting String Array in android from .xml using Resources.getSystem()

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 exception.
i found similar questions that referred that problem here Why can't I use Resources.getSystem() without a Runtime error?. but i didnt 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);      }    }    

i need help in two things-
1. can somebody please help me to resolve this problem and explain the solution?
2. i didnt 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.

No comments:

Post a Comment