I am new to android programming and it’s been some years since I have done any Java programming.
I have declared a string-array in the XML strings.xml:
<string-array name="questionSet1_array">
<item>Question Introduction</item>
<item> Question1</item>
<item>Question 2</item>
</string-array>
I then start an Activity called Coastal. Then, I begin a second activity by pushing a button, starting the activity Foreshore.
In activity Foreshore, I make the following declaration:
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Foreshore extends Activity {
//globally declare parameters
private TextView text;
private EditText answerEntry;
private Button next;
Resources res = getResources();
String [] theQuestions = res.getStringArray(R.array.questionSet1_array);
//private String theAnswers[];
//private String answerPasser;
private int length;
private int i=0;
The lines Resources res ..., String[] ... cause the program to force close and will not let the activity Foreshore commence. When I remove these lines the activity starts correctly.
What is causing this forced close?
Thanks
You can’t call
getResources()before your activity class is “officially” instantiated (it’s an instance method, and at that point you don’t have an instance yet). Move your String array initialization to youronCreate().