我是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
有什么想法吗?
答案 0 :(得分:2)
好吧,也许我误解了,但我认为这只是教程中的一个错字。
getResources().getInteger()
应采用integer
类型的资源值,例如:
getResources().getInteger(R.integer.my_int_value_key);
将在某些值XML中定义,例如:
<integer name="my_int_value_key">100</integer>
答案 1 :(得分:1)
您在values.xml中没有值,其名称存储在R.string.saved_high_score_default中。
在我看来,这是一种过于复杂的方式。我将使用java常量作为默认值(私有final int DEFAULT_VALUE),而不是在大多数情况下从xml读取它。
答案 2 :(得分:0)
这一行试图从字符串资源中获取一个int:
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
尝试类似:
int defaultValue = Integer.valueOf(getResources().getString(R.string.saved_high_score_default));