简体   繁体   中英

unable to call upon resources (particularly a string array) - java / android

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM