0

In my app I use an array of drawable ids. It's an XML file saved at res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
</resources>

Then I retrive it using this code:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

But I get an error saying: array "cannot be resolved or is not a field".

So How do I get an array of ints that contains the ids from the xml file?

Thanks :)

0

4 Answers 4

1

Try cleaning your project. Project - Clean... And check your imports. Do CTRL + Shift + O

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

Comments

1

You should use a typed array in arrays.xml file within your res folder that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </string-array>

</resources>

Then in your activity access them like so:

TypedArray imgs = getResources().obtainTypedArray(R.array.icons);
//get resourceid by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));

Comments

0

im wondering maybe should you have it declared in res/values/strings.xml like

<string-array name="numbers_array">
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
</string-array>

and then sunstitute your ids for the numbers in the array above...

Comments

0

My code was ok. In my res dir there was an image with a capital letter in it's name! and that's Invalid file name which must contain only [a-z0-9_.] :) So that's why there was an error in the R class. Thanks you guys!

Comments

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.