Answer the question
In order to leave comments, you need to log in
How to fix error with context?
I have a class Questions in which I initialize arrays of strings.
Because need a context to get the Strings Resources, I pass it to the constructor when I instantiate the class in the MainActivity activity:
MainActivity.java
HashMap<String, String[]> questions = new HashMap(); // словарь: ключ - строка, значение - массив строк из Questions.java
Questions q = new Questions(this);
questions.put(getResources().getString(R.string.question1), q.arr1);
questions.put(getResources().getString(R.string.question2), q.arr2);
questions.put(getResources().getString(R.string.question3), q.arr3);
package ru.examplle.pravmin;
import ru.examplle.pravmin.R;
import android.content.Context;
public class Questions {
private Context context;
public Questions(Context cur) {
this.context = cur;
}
String[] arr1 = {context.getResources().getString(R.string.answer1_for_question1), context.getResources().getString(R.string.answer2_for_question1), context.getResources().getString(R.string.answer3_for_question1), context.getResources().getString(R.string.answer4_for_question1), context.getResources().getString(R.string.right_answer_for_question1), context.getResources().getString(R.string.comment_for_question1)};
String[] arr2 = {context.getResources().getString(R.string.answer1_for_question2), context.getResources().getString(R.string.answer2_for_question2), context.getResources().getString(R.string.answer3_for_question2), context.getResources().getString(R.string.answer4_for_question2), context.getResources().getString(R.string.right_answer_for_question2), context.getResources().getString(R.string.comment_for_question2)};
String[] arr3 = {context.getResources().getString(R.string.answer1_for_question3), context.getResources().getString(R.string.answer2_for_question3), context.getResources().getString(R.string.answer3_for_question3), context.getResources().getString(R.string.answer4_for_question3), context.getResources().getString(R.string.right_answer_for_question3), context.getResources().getString(R.string.comment_for_question3)};
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at ru.examplle.pravmin.Questions.<init>(Questions.java:15) // строка String[] arr1 = {context.getResources().getString(R.string.answer1_for_question1), context.getResources().getString(R.string.answer2_for_question1), context.getResources().getString(R.string.answer3_for_question1), context.getResources().getString(R.string.answer4_for_question1), context.getResources().getString(R.string.right_answer_for_question1), context.getResources().getString(R.string.comment_for_question1)};
at ru.examplle.pravmin.MainActivity.onCreate(MainActivity.java:116) // строка Questions q = new Questions(this);
Answer the question
In order to leave comments, you need to log in
Try to implement the task a little differently ...
I understand that the answers to the questions are a constant? Instead of writing each answer to a question in the strings.xlm file as a separate constant, write an array of strings there:
<string-array name="answers_for_question_1">
<item>Answer1</item>
<item>Answer2</item>
<item>Answer3</item>
...
<item>AnswerN</item>
</string-array>
List<String> answers_for_q1 = Arrays.asList(getResources().getStringArray(R.array.answers_for_question_1));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question