Reputation: 619
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 :)
Upvotes: 0
Views: 668
Reputation: 619
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!
Upvotes: 0
Reputation: 2513
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));
Upvotes: 1
Reputation: 372
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...
Upvotes: 0
Reputation: 230
Try cleaning your project. Project - Clean... And check your imports. Do CTRL + Shift + O
Upvotes: 1