1
votes

In my application I have the following line:

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

This is throwing an exception while running the application. This only occurs in version 7.1 and 8 of Android, in the previous normal wheel, does anyone know why?

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{insidetechnology.studio.ostdor.anestesiaanimal/insidetechnology.studio.ostdor.anestesiaanimal.autenticacao.state}: java.lang.IndexOutOfBoundsException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.IndexOutOfBoundsException at android.content.res.StringBlock.nativeGetString(Native Method) at android.content.res.StringBlock.get(StringBlock.java:82) at android.content.res.AssetManager.getResourceValue(AssetManager.java:236) at android.content.res.AssetManager.getResourceText(AssetManager.java:164) at android.content.res.Resources.getText(Resources.java:334) at android.content.res.Resources.getString(Resources.java:432) at insidetechnology.studio.ostdor.anestesiaanimal.autenticacao.state.onCreate(state.java:38) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1
Where do you call getResources? Maybe you call this before it is made available?Matt Clark
@MattClark in onCreate, but this work in old versions :/Woton Sampaio

1 Answers

1
votes

A workaround can be using string-array instead of string

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

then

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

I dont know why but maybe your string exceeds the limitation of string resource in new Android versions