Neta Meta Neta Meta - 1 month ago 9
Android Question

trying to retrieve a resources but for some reason an error is being thrown

I am new to android development. i am following their tutorial at: android development

Now when i do

int defaultValue = getResources().getInteger(R.string.saved_high_score_default);


i am getting an error however when i use:

int defaultValue = (int)R.string.saved_high_score_default;


It works perfectly fine.

here is the error i get:

08-25 07:38:13.228: E/AndroidRuntime(3436): FATAL EXCEPTION: main
08-25 07:38:13.228: E/AndroidRuntime(3436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.fragments/com.example.android.fragments.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f040003 type #0x3 is not valid
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread.access$700(ActivityThread.java:143)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.os.Looper.loop(Looper.java:137)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread.main(ActivityThread.java:4953)
08-25 07:38:13.228: E/AndroidRuntime(3436): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 07:38:13.228: E/AndroidRuntime(3436): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 07:38:13.228: E/AndroidRuntime(3436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
08-25 07:38:13.228: E/AndroidRuntime(3436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
08-25 07:38:13.228: E/AndroidRuntime(3436): at dalvik.system.NativeStart.main(Native Method)
08-25 07:38:13.228: E/AndroidRuntime(3436): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f040003 type #0x3 is not valid
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.content.res.Resources.getInteger(Resources.java:838)
08-25 07:38:13.228: E/AndroidRuntime(3436): at com.example.android.fragments.HeadlinesFragment.onStart(HeadlinesFragment.java:60)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.Fragment.performStart(Fragment.java:1332)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1815)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:518)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.Activity.performStart(Activity.java:5170)
08-25 07:38:13.228: E/AndroidRuntime(3436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
08-25 07:38:13.228: E/AndroidRuntime(3436): ... 11 more


Any idea ?

Answer

Okay, maybe I'm misunderstanding, but I think that's just a typo in the tutorial.

getResources().getInteger() should take a resource value of type integer, for example:

getResources().getInteger(R.integer.my_int_value_key);

Which would be defined in a values XML somewhere like:

<integer name="my_int_value_key">100</integer>