我不断收到来自市场的以下错误报告:
java.lang.StringIndexOutOfBoundsException: length=51; regionStart=37; regionLength=-15 at
java.lang.String.startEndAndLength(String.java:593) at
java.lang.String.getChars(String.java:902) at
android.text.SpannableStringInternal.getChars(SpannableStringInternal.java:70)
at android.text.TextUtils.getChars(TextUtils.java:70) at
android.text.TextLine.set(TextLine.java:160) at
android.text.Layout.draw(Layout.java:424) at
android.widget.TextView.onDraw(TextView.java:5262) at
android.view.View.draw(View.java:11184) at
android.view.ViewGroup.drawChild(ViewGroup.java:2892) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.widget.ListView.drawChild(ListView.java:3231) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.widget.AbsListView.dispatchDraw(AbsListView.java:2277) at
android.widget.ListView.dispatchDraw(ListView.java:3226) at
android.view.View.draw(View.java:11289) at
android.widget.AbsListView.draw(AbsListView.java:3760) at
android.view.ViewGroup.drawChild(ViewGroup.java:2892) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.View.draw(View.java:11187) at
android.widget.FrameLayout.draw(FrameLayout.java:450) at
com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2291)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2210) at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816) at
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:4511) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) at
dalvik.system.NativeStart.main(Native Method)
令人沮丧的部分是,这源于 android 本身——并且在我的代码中没有起点。在做了一些研究之后,这只发生在希伯来语用户身上,我发现这个链接到谷歌错误 - http://code.google.com/p/android/issues/detail?id=30889
我自然而然地尝试联系作者和分配给这个 bug 的谷歌员工——他们都没有屈膝礼或回复。
我设置跨度的相关代码是:
if(searchTerm!=null){
try{
Spannable str = (Spannable) name.getText();
int s_ind = fi.getSearchName().indexOf(searchTerm);
str.setSpan(new ForegroundColorSpan(Color.BLACK),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(Color.YELLOW),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}catch(Exception ex){
FLogger.logE(DW_CODES.PREDEFINED_ERROR, "Exception while searching for view in predefined "+ex.getMessage());
}
}
搜索词是名称的一部分。我想用不同的颜色标记 searchTerm 部分,以便用户更容易看到它。
我为此投入了很多时间,因为我什至没有设法在我的 4 台测试设备和 8 台 AVD 上重新创建它,我有点迷失了。任何提示\建议在没有 Spannables 的情况下做同样的事情,将不胜感激。