我遇到了不寻常的问题。当我访问activity
(LevelTwoActivity)中的字符串资源时,它会给我以下问题。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package/com.package.LevelTwoActivity}: java.lang.IndexOutOfBoundsException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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.Context.getText(Context.java:543)
at com.package.LevelTwoActivity.loadArraysFromResources(LevelTwoActivity.java:1586)
at com.package.LevelTwoActivity.onCreate(LevelTwoActivity.java:133)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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)
E/Jellow: exception caught
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dsource.idc.jellowintl/com.package.LevelTwoActivity}: java.lang.IndexOutOfBoundsException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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.Context.getText(Context.java:543)
at com.package.LevelTwoActivity.loadArraysFromResources(LevelTwoActivity.java:1586)
at com.package.LevelTwoActivity.onCreate(LevelTwoActivity.java:133)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
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)
代码中的行如下
LevelTwoVerbiageModel verbiageModel = new Gson()
.fromJson(getString(R.string.levelTwoVerbiage), LevelTwoVerbiageModel.class);
字符串reosurce如下:
<string name="levelTwoVerbiage">
{\"arrays\":[[[
\"I like to greet others\",
\"I really like to greet others\",
\"I want to greet others\"],[],[],
....
....
\"I really don’t want\",
\"I don’t feel very safe\",
\"I really don’t feel very safe\"
]]]}
</string>
我访问的资源是json字符串,长度约为135000个字符。它存储在values文件夹下,但存储在名为arrays-level-two.xml的文件中。 以一种奇怪的方式,代码一直在工作,直到我更新了我的测试设备。现在,它没有用。该代码在具有不同api级别的其他测试设备上完美运行。
失败的设备详细信息是:
Device name: MI A1
Android version: 8.0.0
Android patch level: 1 April 2018
Build number: OPR1.170623.026.V9.5.10.0.DHMIFA
我无法确定访问字符串资源的原因:java.lang.IndexOutOfBoundsException.
有人有任何指针出错吗?
提前致谢。 -Rahul
答案 0 :(得分:1)
@Rahul - 我不知道为什么Android Oreo会抛出此异常。这可能是由于字符串资源中使用的字符数量增加了限制。使用大约135000个字符的字符串对我来说也是一种不好的做法。相反,我建议您使用String[]
,然后在代码中将其作为字符串数组<string-array name="my_items">
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
...
</string-array>
,然后使用它。
这样你甚至不需要使用Gson来解析Json。
示例:
的strings.xml
String[] myItems = getResources().getStringArray(R.array.my_items);
MyFile.java
myItems[index_you_want_to_access]
将其用作connect