0

我是android开发的新手。我正在关注他们的教程:android development

现在当我做

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

但是当我使用时出现错误:

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

它工作得很好。

这是我得到的错误:

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

任何的想法 ?

4

3 回答 3

2

好吧,也许我理解错了,但我认为这只是教程中的一个错字。

getResources().getInteger()应该采用 type 的资源值integer,例如:

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>
于 2013-08-25T05:47:14.743 回答
1

您在 values.xml 中没有名称存储在 R.string.saved_high_score_default 中的值。

在我看来,这是一种过于复杂的方法。在大多数情况下,我将使用 java 常量作为默认值(私有 final int DEFAULT_VALUE),而不是从 xml 中读取它。

于 2013-08-25T05:42:48.873 回答
0

此行试图从字符串资源中获取 int:

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

尝试类似:

    int defaultValue = Integer.valueOf(getResources().getString(R.string.saved_high_score_default));
于 2013-08-25T05:46:42.727 回答