Android IndexOutOfBound Exception for resource getString

一笑奈何 提交于 2020-01-02 03:52:25

问题


Note: This is specific problem with android system, not simply out of bound exception which some suggesting duplicate

I have a problem with investigating the cause of the exception. I'm trying to get string resource text(which is HTML):

val string = getString(R.string.long_html_text)

This HTML is of course valid(works on emulator and other real devices) but not on real device LGE Nexus 5X API 27. The html text has 40113 characters.

When I am trying to get this string resource as above I am getting this Exception:

   04-17 14:38:17.136 2847-2847/com.myapp.tmt.app.myapp E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.myapp.tmt.app.myapp, PID: 2847
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.tmt.app.myapp/com.myapp.tmt.app.myapp.ui.eula.EulaActivity}: java.lang.IndexOutOfBoundsException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    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:335)
       at android.content.res.Resources.getString(Resources.java:433)
       at android.content.Context.getString(Context.java:556)
       at com.myapp.tmt.app.myapp.ui.eula.EulaActivity.prepareLicence(EulaActivity.kt:33)
       at com.myapp.tmt.app.myapp.ui.eula.EulaActivity.onCreate(EulaActivity.kt:28)
       at android.app.Activity.performCreate(Activity.java:6999)
       at android.app.Activity.performCreate(Activity.java:6990)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
       at android.os.Handler.dispatchMessage(Handler.java:106) 
       at android.os.Looper.loop(Looper.java:164) 
       at android.app.ActivityThread.main(ActivityThread.java:6494) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Has anyone encounter it?


回答1:


As this was only happening on Android Oreo devices i resolve this issue only by split my very long HTML string to two parts.

I think that this issue was due to new Android security restrictions (as some suggested here)



来源:https://stackoverflow.com/questions/49878650/android-indexoutofbound-exception-for-resource-getstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!