0

I have created a string array, but am unable to access it in my java. I'm pretty new to java, so any help would be greatly appreciated!

This is part of an android app that will use the string array for captions below a bunch of pictures in a series.

Here's what I have. If I comment out these two lines it still runs:

Resources res=getResources();
String[] captionArray=res.getStringArray(R.array.chapter0captions);

This is part of a class that is used for storing the images and captions i wish to open using the Activities that are my menus (not sure if i explained that well...) I can't figure out what is wrong with those two lines, but I really would like to call upon my array instead of having to copy paste under each part of the switch-case code. Thanks in advance for your help!

import android.app.Application;
import android.content.res.Resources;

public class PositionStorage extends Application {
    public static int imageToOpen;
    public static String captionToOpen;
    public static String imageToOpenString;
    Resources res=getResources();
    String[] captionArray=res.getStringArray(R.array.chapter0captions);
public void storeTheString(int pos){
        int imageNumber=pos+1;
     switch (imageNumber) {
        case 1:
            imageToOpen=0x7f020005;
             captionToOpen=captionArray[pos];
            break;
        case 2:
            imageToOpen=0x7f020010; 
            captionToOpen=captionArray[pos];
            break;
        case 3:
            imageToOpen=0x7f02001b;
            captionToOpen=captionArray[pos];
            break;
5
  • What is the error if you put those two? And what's in R.array.chapter0captions? Can you put what's in R.array.chapter0captions as part of the question? Commented Sep 3, 2011 at 4:12
  • What is the error you get when you uncomment the code where you access the string array? It could be that R.array.chapter0captions is not defined. If you're sure a resource with this name exists, try cleaning and rebuilding your project. Sometimes that is necessary for the resource file to get fully updated. Commented Sep 3, 2011 at 4:14
  • R.array.chapter0captions was defined in my strings.xml file. It is a very long string-array, but the basic layout is.... <string-array name="chapter0captions"> <item>FIG. 1</item> <item>FIG. 2</item></string-array> Commented Sep 3, 2011 at 4:49
  • And the error I get is a force quit when I run the app in the emulator....I might need some help figuring out how to use the debugger....logcat says GC_Explicit freed *** Commented Sep 3, 2011 at 4:54
  • debugger says this...... Thread [<1> main] (Suspended (exception NullPointerException)) PositionStorage(ContextWrapper).getResources() line: 80 PositionStorage.<init>() line: 10 Chapter0Menu.onListItemClick(ListView, View, int, long) line: 32 ListActivity$2.onItemClick(AdapterView, View, int, long) line: 321 ListView(AdapterView).performItemClick(View, int, long) line: 284 ListView.performItemClick(View, int, long) line: 3382 AbsListView$PerformClick.run() line: 1696 .......it goes on, but does that help?? Commented Sep 3, 2011 at 5:00

1 Answer 1

1

Don't declare String array into the string.xml file.Instead declare the string array in the arrays.xml under the values folder.

the Structure is

res>>values>>arrays.xml

In the arrays.xnl

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="xxxarray">
    <item>one</item>
    <item>two</item>
    <item>thre</item>
</string-array>

in your activity

String[] arrayvalues=getResources().getStringArray(R.array.xxxarray);

if you declare the array in the string.xml then the system treats that as the String.

Sign up to request clarification or add additional context in comments.

1 Comment

this did not work, and I don't think it makes a difference because in my R.java file it showed up under R.array.chapter0captions regardless of the xml file it was written in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.