Null Pointer Exception when calling getResources()

前端 未结 3 702
长情又很酷
长情又很酷 2021-01-17 06:08

I\'m making a quiz program for android, and to keep things compatible for different languages, I\'ve put all my quiz questions and labels in my strings.xml file. Basically

3条回答
  •  萌比男神i
    2021-01-17 06:32

    For those that are interested, I got my code up and running with the following.

    public class MainActivity extends AppCompatActivity {
    
        String[] questionHeaders, answerKey, questions;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Resources res = getResources();
            questionHeaders = res.getStringArray(R.array.question_header_array);
            answerKey = res.getStringArray(R.array.answer_key_array);
            questions = res.getStringArray(R.array.questions_array);
        }
    
        int correctAnswers = 0;
        int questionCounter = 0;
    
    1. Declared the three String[] arrays in the main activity before onCreate().
    2. Initialize a 'Resources' object named res inside onCreate().
    3. Call getStringArray on res to load each String[] array

提交回复
热议问题