`getResources()。getString`生成异常

时间:2018-07-03 01:39:59

标签: android

在我的应用程序中,有以下一行:

String ci = getResources().getString(R.string.cidades);

这在运行应用程序时引发异常。这仅发生在Android的7.1和8版本中,在以前的普通模式下,有人知道为什么吗?

Logcat:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {insidetechnology.studio.ostdor.anestesiaanimal / insidetechnology.studio.ostdor.anestesiaanimal.autenticacao.state}:java.lang.IndexOutOfBoundsException                                                                                                     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)                                                                                                     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                                                     在android.app.ActivityThread.-wrap11(未知来源:0)                                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                                                     在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                                     在android.os.Looper.loop(Looper.java:164)                                                                                                     在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                                     在java.lang.reflect.Method.invoke(本机方法)                                                                                                     在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                                                  造成原因:java.lang.IndexOutOfBoundsException                                                                                                     在android.content.res.StringBlock.nativeGetString(本地方法)                                                                                                     在android.content.res.StringBlock.get(StringBlock.java:82)                                                                                                     在android.content.res.AssetManager.getResourceValue(AssetManager.java:236)                                                                                                     在android.content.res.AssetManager.getResourceText(AssetManager.java:164)                                                                                                     在android.content.res.Resources.getText(Resources.java:334)                                                                                                     在android.content.res.Resources.getString(Resources.java:432)                                                                                                     在insidetechnology.studio.ostdor.anestesiaanimal.autenticacao.state.onCreate(state.java:38)                                                                                                     在android.app.Activity.performCreate(Activity.java:6975)                                                                                                     在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)                                                                                                     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)                                                                                                     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                                                     在android.app.ActivityThread.-wrap11(未知来源:0)                                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                                                     在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                                     在android.os.Looper.loop(Looper.java:164)                                                                                                     在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                                     在java.lang.reflect.Method.invoke(本机方法)                                                                                                     在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                                     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:1)

一种解决方法可以使用string-array代替string

<string-array name="items">
<item>item0</item>
<item>item1</item>
...
</string-array>

然后

String[] items = getResources().getStringArray(R.array.items);

我不知道为什么,但是也许您的字符串超出了新Android版本中字符串资源的限制