-1

Through adding and removing certain parts of my app, I was able to determine that the following lines creating java fields for string codes was causing a crash in my app (starting with a white screen, then goes black and says the app is not responding);

private final String HIGH_SCORE = getString(R.string.highscore_code);
private final String CURRENT_SCORE = getString(R.string.currentscore_code);

This is my strings.xml file, which I expected to be the source of these strings using this code from the research I conducted online;

<resources>
    <string name="app_name">My App</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_my_menu">MyMenu</string>

    <string name="highscore_code">HIGH_SCORE</string>
    <string name="currentscore_code">LAST_SCORE</string>
</resources>

I would highly appreciate if anyone here knows how to correctly load the strings.

Super Hacker
  • 124
  • 2
  • 3
  • 10
  • post your stacktrace – injecteer Aug 01 '15 at 16:43
  • where are you calling the getString() in your code. – rahul.ramanujam Aug 01 '15 at 16:44
  • Sorry, but what is a stacktrace? I'm a little new to Android Studio – Super Hacker Aug 01 '15 at 16:46
  • Apology accepted. Read this: http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors pro tip: try to google before you're asking, you'd be surprised how many questions will be answered by google ;) – Nir Alfasi Aug 01 '15 at 16:47
  • the java code is being called in my MainGame class, which inherits from Activity. It is my major Activity which loads upon the app starting. – Super Hacker Aug 01 '15 at 16:47
  • I don't think I have a stack-trace as I am testing by simply downloading the application onto my phone and then installing and running it – Super Hacker Aug 01 '15 at 16:50
  • @superhacker if your device is connected to system and detected by Android Studio then you can refer the logcat (type :error highlighted in red) which will give you complete stacktrace – Shadow Droid Aug 01 '15 at 16:55

3 Answers3

1

Try this,

Resources res=c.getResources();
String name=res.getString(R.string.name);

Where c is the Context object.

Parag Kadam
  • 3,620
  • 5
  • 25
  • 51
  • by c I assume you mean the current class? – Super Hacker Aug 01 '15 at 16:55
  • No, it's the **Context**. – Phantômaxx Aug 01 '15 at 16:55
  • In which method are you executing these statements? private final String HIGH_SCORE = getString(R.string.highscore_code); private final String CURRENT_SCORE = getString(R.string.currentscore_code); – Parag Kadam Aug 01 '15 at 16:57
  • would getApplicationContext() be sufficient for that? – Super Hacker Aug 01 '15 at 16:58
  • the statements are being executed in that part between the class name declaration and the constructor, where you declare fields (I feel like such a noob writing this) – Super Hacker Aug 01 '15 at 16:59
  • You cannot write code statements at places where you declare fields, aren't you getting any compile time error. If you are not getting any compile time errors then you are writing it inside a method , I am asking which method is that. You know what a method is right? – Parag Kadam Aug 01 '15 at 17:04
  • If you are inside a `Fragment`, you must get the Fragment's **Activity's Context**: `getActivity().getApplicationContext()` – Phantômaxx Aug 01 '15 at 17:05
  • I indeed know what a method is. It is definitely where you declare fields. Btw, my mistake if it makes a difference - there is no constructor, the fields are declared between the class name declaration and the onCreate method – Super Hacker Aug 01 '15 at 17:08
  • thanks for the help guys. It turns out the correct solution was to obtain the strings in the onCreate method – Super Hacker Aug 01 '15 at 17:12
  • This is because onCreate() is a Activity class method and the instance of Context is readily available here. Cheers :) – Parag Kadam Aug 01 '15 at 17:15
1

You are defining the fields HIGH_SCORE and CURRENT_SCORE as class fields. Here you cannot access the method getString() without a valid Context Reference since the method is definded in the Context class. So you need to call it after the onCreate() call back in an Activity.

Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
1

getString(...) needs a properly initialized android component (Fragment, Activity, Service, ....). That is it must be in the right life cycle state.

For activities that is when or after onCreate and before onDestroy is called. For fragments that would be onAttach and onDetach, and so on. You should probably google for Activity life cycle or Fragment life cycle.

So you cannot use getString() to initialize class constants.

You can set variables by overriding onCreate in an Activity.

private String highScore;
private String currentScore;

@override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(<your_layout>);

    highScore = getString(R.string.highscore_code);
    currentScore = getString(R.string.currentscore_code);
}
Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37