-1

I want to use a string resource as defined in Strings.xml as follows:

    <string name="game_name_nassau2">Nassau2</string>

when I instantiate an object that is epecting a String argument. Using a literal, I say:

private Game lGame = new Game(null,"Nassau2", lpl, 100, true, false, false, false);

and this works fine. Trying to use the string resource, I say:

private Game lGame = new Game(null, getResources().getString(R.string.game_name_nassau2), lpl, 100, true, false, false, false);

and this crashes the app. (Crashes, meaning I get a "Source Not Found" error I think at the instantiation). I'm trying to be a good guy and not embed strings into code. Here's the LogCat errors...

    11-20 12:31:18.105: E/AndroidRuntime(7158): FATAL EXCEPTION: main
11-20 12:31:18.105: E/AndroidRuntime(7158): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for com.aci.golfgames; is package not installed?
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.LoadedApk.makeApplication(LoadedApk.java:509)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.os.Looper.loop(Looper.java:137)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at java.lang.reflect.Method.invoke(Method.java:525)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at dalvik.system.NativeStart.main(Native Method)
11-20 12:31:18.105: E/AndroidRuntime(7158): Caused by: java.lang.IllegalStateException: Unable to get package info for com.aci.golfgames; is package not installed?
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:369)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
11-20 12:31:18.105: E/AndroidRuntime(7158):     at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
11-20 12:31:18.105: E/AndroidRuntime(7158):     ... 11 more

All I changed was the line

    private Game lGame = new Game(null,"Nassau2", lpl, 100, true, false, false, false);

to

    private Game lGame = new Game(null, getResources().getString(R.string.game_name_nassau2), lpl, 100, true, false, false, false);

iow, all I changed was "Nassau2" to getResources().getString(R.string.game_name_nassau2). I set a breakpoint on the first executable line in the activity and it never gets there.

What am I doing wrong?

Thanks

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
ddk
  • 91
  • 3
  • 11

2 Answers2

0

and this crashes the app.

It's not clear what you mean by that, but you should use .equals instead of == when comparing Strings

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

Perhaps I was not clear when I said that I was using

private Game lGame = new Game(null, getResources().getString(R.string.game_name_nassau2), lpl, 100, true, false, false, false);

in the instantiation, it was outside of the onCreate or any other method within the object. Therefore, I had no context and the getResources().getString(...) requires a context. It seems that without some sort of work-around to get a context, this will not work for application resources.

ddk
  • 91
  • 3
  • 11
  • It's just activity lifecycle. The activity is not initialized to be a working Context until `onCreate()`. Member variable initialization during object instantiation is too early. – laalto Nov 24 '13 at 19:50
  • Since I want the object `lGame` available to the entire activity, I should declare it where I did (in the object instantiation) and initialize it at the first available moment within a context (i.e., just after the `onCreate()`. Is this correct? – ddk Nov 25 '13 at 14:18
  • Yes, declaring as member var and initializing in onCreate is ok. – laalto Nov 25 '13 at 15:07