You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android新手求助:无错误行号的IndexOutOfBoundsException问题

Fixing java.lang.IndexOutOfBoundsException in TextView/Button Text Rendering

Hey there! That crash you're seeing—IndexOutOfBoundsException pointing to Paint.getRunAdvance—is a text rendering issue, usually tied to how you're setting content on your buttons or a mismatch in your data setup. Let's walk through how to fix this while keeping your current implementation intact.

First, Let's Diagnose the Possible Culprits

Looking at your code and stack trace, a few things stand out:

  • Hardcoded TotalChapters: You've set this to 2, which matches your current array lengths, but it's risky if you ever update your string arrays later.
  • Split text setting: Using setText() followed by append() with Html.fromHtml() might be creating a text structure that causes rendering glitches under the hood.
  • Font loading risk: If your custom font file isn't properly loaded, it could throw off text measurement and lead to this crash.

Step-by-Step Fixes

1. Replace Hardcoded Chapter Count with Dynamic Array Length

Instead of forcing TotalChapters = 2, pull the length directly from your string arrays to ensure they always match. This prevents future mismatches if you add more chapters:

// Remove the hardcoded int TotalChapters = 2;
private void initializeData() {
    ChapterName = getResources().getStringArray(R.array.ChapterName);
    ChapterNumber = getResources().getStringArray(R.array.ChapterNumber);
    
    // Add a check to make sure both arrays are the same length
    if (ChapterNumber.length != ChapterName.length) {
        Log.e("CategoryList", "Error: ChapterNumber and ChapterName arrays have different lengths!");
        return;
    }
    TotalChapters = ChapterNumber.length; // Use the actual array length
    
    // Rest of your code...
}

2. Set Button Text in One Go (Avoid append())

Combining your text into a single HTML string and setting it all at once avoids potential text structure issues that cause rendering crashes. Replace your current button text lines with this:

// Replace these two lines:
// ChapterButton[start].setText(ChapterNumber[start]);
// ChapterButton[start].append(Html.fromHtml("<br/>" + ChapterName[start]));

// Use a single HTML string instead
String buttonText = String.format("%s<br/>%s", ChapterNumber[start], ChapterName[start]);
// For Android 7.0+, use Html.FROM_HTML_MODE_LEGACY to avoid deprecation warnings
ChapterButton[start].setText(Html.fromHtml(buttonText, Html.FROM_HTML_MODE_LEGACY));

3. Safeguard Custom Font Loading

If your ERASMD.TTF file is missing, misnamed, or not in the assets folder, it can silently fail and cause text rendering issues. Add a try-catch to handle font loading gracefully:

try {
    font = Typeface.createFromAsset(getAssets(), "ERASMD.TTF");
} catch (Exception e) {
    e.printStackTrace();
    // Fallback to default font if custom one fails to load
    font = Typeface.DEFAULT;
}

4. Clean and Rebuild Your Project

Sometimes Android Studio's cached files cause weird, unexplained crashes. Go to Build > Clean Project, then Build > Rebuild Project to clear old artifacts.

Why This Works

  • Dynamic array length ensures you never loop beyond the bounds of your string arrays, even if you update them later.
  • Setting text in one go creates a consistent text structure that Android's rendering system can handle without index errors.
  • Safeguarding font loading prevents hidden issues that mess with text measurement and drawing.

Give these steps a try—this should resolve that IndexOutOfBoundsException while keeping your current implementation intact.

内容的提问来源于stack exchange,提问作者user3686177

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:24:56