TotalActivity启动失败:StringIndexOutOfBoundsException(-2)问题排查求助
Hey there, let's dig into this crash you're hitting when launching TotalActivity. The core issue here is the StringIndexOutOfBoundsException: 字符串索引超出范围: -2, which traces back to a call to String.substring(). Let's break down why this happens and how to fix it.
异常根源解析
This error pops up when you call substring() with an invalid index value—specifically, an index that's negative (like -2 here) or larger than the length of the string. The most common trigger for this exact scenario is:
- You're using
lastIndexOf()orindexOf()to locate a character/segment in a target string - That character/segment doesn't exist in the string, so the method returns
-1 - You then do arithmetic on that
-1(like subtracting 1 to get-2) and pass it directly tosubstring()
For example, if you had code like this:
String orderData = getIntent().getStringExtra("total_info"); int splitPos = orderData.lastIndexOf(','); // If there's no ',' in orderData, splitPos becomes -1, so splitPos -1 = -2 String amountPart = orderData.substring(0, splitPos - 1);
This would throw the exact error you're seeing.
解决步骤与代码修复
Follow these steps to fix the issue:
1. Locate the problematic code
First, track down all calls to substring() in TotalActivity's onCreate() method (or any code that runs when the activity initializes—like intent data processing or initialization blocks). This is where the crash is originating.
2. Add defensive checks for index values
Before using the result of indexOf() or lastIndexOf(), always verify that it's not -1 (meaning the target character/segment was found). Also ensure any derived indices are non-negative and don't exceed the string's length.
Here's a fixed version of the example above:
String orderData = getIntent().getStringExtra("total_info"); // First check if the string itself isn't null or empty if (orderData != null && !orderData.isEmpty()) { int splitPos = orderData.lastIndexOf(','); // Only proceed if we found the ',' and the derived index is valid if (splitPos != -1 && (splitPos - 1) >= 0) { String amountPart = orderData.substring(0, splitPos - 1); // Continue processing the value } else { // Handle the case where there's no ',' (or the index would be invalid) // Use a default value or show a user-friendly message String amountPart = "0.00"; } } else { // Handle missing or empty intent data Toast.makeText(this, "Invalid order data received", Toast.LENGTH_SHORT).show(); finish(); // Optional: close the activity if data is critical }
3. Validate incoming intent data
Since this crash happens on activity launch, double-check that the data being passed to TotalActivity via Intent follows your expected format. Add validation in the part of your app that sends this intent to prevent malformed or empty strings from reaching TotalActivity.
Quick Tips
- Never assume intent extras or user-provided strings will match your expected format—always validate input first.
- Use Android's
TextUtils.isEmpty()to check for null/empty strings in one go. - If you're processing numbers or currency strings, consider using
DecimalFormatorNumberFormatinstead of manual substring operations—they handle edge cases like missing separators automatically.
内容的提问来源于stack exchange,提问作者androidter

