我们的崩溃日志记录系统显示了崩溃,我无法理解它是如何发生的。用户输入未知。我已经添加了日志记录,但是结果只有在下一个版本(约2周)之后才能看到。
下面的代码如何引发下一个异常:
Crashes with java.lang.StringIndexOutOfBoundsException length=0; index=1
不是因为并发问题?换句话说,它可能会在什么情况下崩溃以及如何避免(不将其包装到try-catch
中)?如果这是一个并发问题(我看不到,但也许我错了)怎么办?
private String contactName; // has getter and setter
public String getFirstSymbol() {
String firstSymbol = "";
String trimmed = contactName.trim();
if (!TextUtils.isEmpty(trimmed)) { // TextUtils.isEmpty is Android method: return str == null || str.length() == 0;
String[] parts = trimmed.toUpperCase().split("\\s+");
firstSymbol = parts[0].substring(0, 1); // crashes here:
}
return firstSymbol;
}
上周在7台设备上发生了约100次崩溃。
这是堆栈跟踪
Fatal Exception: java.lang.StringIndexOutOfBoundsException: length=0; index=1
at java.lang.String.substring(String.java:1971)
at my.app.model.Contact.getFirstSymbol(Contact.java:256)
at my.app.ui.AvatarView.bind(AvatarView.java:73)
at my.app.viewholder.CallLogViewHolder.bind(CallLogViewHolder.java:50)
at my.app.viewholder.CallLogViewHolder.bind(CallLogViewHolder.java:23)
at my.app.ContactListAdapter.onBindViewHolder(ContactListAdapter.java:68)
at my.app.ContactListAdapter.onBindViewHolder(ContactListAdapter.java:25)
at androidx.recyclerview.widget.RecyclerView$Adapter.onCreateViewHolder(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.getItemCount(RecyclerView.java:6781)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.findFirstDependency(HeaderScrollingViewBehavior.java:142)
at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1556)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.findFirstDependency(HeaderScrollingViewBehavior.java:142)
at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1556)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:955)
at android.view.View.layout(View.java:20964)
at android.view.ViewGroup.layout(ViewGroup.java:6440)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3092)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2779)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1863)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8072)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
答案 0 :(得分:2)
这里没有线程安全/并发问题。该方法可能与其他线程共享的唯一值是contactName
的值。那是一个String
参考。 String
是不可变的。此方法从(可能)共享的不可变String
派生的其他值均受线程限制。
除此之外,我感到困惑。 trim
应删除\s+
将匹配的任何前导或尾随字符。并且!isEmpty
防护消除了出现空字符串的可能性。因此parts
数组应该至少具有一个元素,而第一个元素应该至少具有一个字符。
但是例外似乎是不同的说法。
因此,这留下了“替代性”的解释,即通常将其视为荒谬的:
String
或Pattern
中存在错误的平台上发生的。这些“替代”解释都没有给您任何前进的方向。因此,如果找不到更好的解释,建议您用try { ... } catch
包围该令人讨厌的代码,并尝试记录触发问题的contactName
字符串的长度和内容。将此内容包含在下一个版本中,如果/当您开始遇到问题时,请准备好发布包含真实修补程序的次要版本。另外,也可以将其视为用户输入的联系人姓名为空...或类似的名称。