1

I have a Android test project. in the resources folder I have strings and colors, but none of the resources are available. I cannot see any of my strings or colors. I have cleaned and rebuilt the project without any luck.

To access a resource I am using:

String test = context.getResources(R.string.button_text);

In this particular case button_text cannot be resolved.

enter image description here

In the strings resource I have four strings

 <string name="button_text">Install</string>
 <string name="button_run_unit_tests">Run All Tests</string>
 <string name="Quit">Quit</string>  
 <string name="IsolatedTest">Run Unit Test</string>

But I cannot access any from my Java resource files. What am I missing?

This is what I see (not sure what it's referencing): enter image description here

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120

2 Answers2

2

What am I missing?

Perhaps you are missing the proper R import. Based on your suggested replacements, it would appear that you are importing the R from your app. Your strings are not in your app. They are in your test project, which has a different package name. Find the package name in your test project's manifest, and try importing that R.

Note that I have never tried putting resources in a test project, and so while it is possible they would work, I cannot be sure of that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

GetResources() gets you an object that allows you to access resources. To access the resource, you then need to do get<Type>() (getString in your case). So you want to do:

String test = context.getResources().getString(R.string.button_text);

Also, you do not strictly need the call to context if you are invoking this in an Activity (since an Activity is a context, you can just do getResources directly).

emerssso
  • 2,376
  • 18
  • 24
  • Either way I cannot see the resource. Plz see my edit. – Roy Hinkley Sep 12 '14 at 23:48
  • Everything else in your post looks correct to me. Is your `strings.xml` properly formatted with a header like: ` `? – emerssso Sep 12 '14 at 23:53
  • Yes, everything is well formed. The project compiles just fine w/o the resource reference line. – Roy Hinkley Sep 12 '14 at 23:54
  • I'm out of ideas then. If you hadn't already cleaned your project, I would swear its using a cache for a different project for the tab completion. Do you have multiple projects open in Eclipse? – emerssso Sep 12 '14 at 23:58