Search code examples
javaandroideclipseandroid-activitygetstring

The method getString(int) is undefined for the type GamePlay


I'm having a problem while using the getString function for reading a string located in the values folder. I get the next error:

The method getString(int) is undefined for the type GamePlay

It is complicated for me because I want to call this function outside the activity and somehow the structure of the project is quite weird.

My project is a game, while you run the game you run SampleGame:

public class SampleGame extends AndroidGame

It is extended from AndroidGame that has the following declaration:

public abstract class AndroidGame extends Activity implements Game

And the SampleGame executes a SplashLoadingScreen that has the following declaration:

public class SplashLoadingScreen extends Screen

The SpalshLoadingScreen finally loads some things and then call the GamePlay

public class GamePlay extends Screen

So the GamePlay is far from being an Activity...

How can I use then the getString(int) function?


Solution

  • For getString(int) you only need a Context object.. You could pass it to your GamePlay object for example having a construcutor GamePlay(Context c), but make sure to pass the Application Context getApplicationContext() if you don't need an Activity context, because the GarbageCollector will not be able to collect your Activity if there is a reference to it's context somewhere.