0

I try to follow the example on the Andoid developers page in order to implement a string array from string resoirces. It doesn't work. The following line of code in my MainActivity(right at the top of the class) is throwing an runtime exception error:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   Resources res = getResources();
   String[] fortuneStrings = res.getStringArray(R.array.fortuneStringArray);
...

I checked everything but I do not know how else to debug. Any help? Thanks.

1. Edit

here is my string.xml

<resources>
  <string-array name = "fortuneStringArray">
    <item>blabla1</item>
    <item>blabla2</item>
    <item>blabla3</item>
    <item>blabla4</item>
  </string-array>
</resources>
JFS
  • 2,992
  • 3
  • 37
  • 48
  • In which method of MainActivity you have this? And what is the cause shown in the exception? – Teemu Lätti Jan 23 '16 at 13:19
  • Can you post whole exception? – arsena Jan 23 '16 at 13:19
  • Use LogCat to examine the Java stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the stack trace, please post it. Note that if these lines are initializers in your class, instead of local variables in a method, that's probably your problem -- you cannot call `getResources()` until inside `onCreate()` or at later points in the life of your activity. – CommonsWare Jan 23 '16 at 13:27

0 Answers0