簡體   English   中英

在某些設備上,運行時錯誤“ x +寬度必須<= bitmap.width()”

[英]Runtime error “x + width must be <= bitmap.width()” on some devices

我正在創建一個程序,該程序實際上是根據Rect和字形創建菜單的。 由於某種原因,此代碼可在模擬器和我的朋友手機上運行,​​但不適用於我的代碼。 我開始相信我的手機可能太舊了(屬於彗星系列)。 但是,像任何優秀的手機應用程序開發人員一樣,我希望我的應用程序可以在任何類型的android手機上運行,​​因此我正在非常徹底地研究此問題。 我收到一條錯誤消息:

由以下原因引起:java.lang.IllegalArgumentException:x + width必須為<= bitmap.width()

我唯一使用位圖的地方是在此處發布的字形類中:

package text;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Glyphs {

private Bitmap bitmap;

private Map<Character, Bitmap> glyphs = new HashMap<Character, Bitmap>(62);

private int width;
private int height;

private char[] charactersL = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private char[] charactersU = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private char[] numbers = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

public int getWidth(){
    return width;
}

public int getHeight(){
    return height;
}

public Glyphs(Bitmap bitmap){
    super();
    this.bitmap = bitmap;
    this.width = 12;
    this.height = 18;

    for(int i = 0; i < 26; i++){
        glyphs.put(charactersL[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 0, width, height));
    }

    for(int i = 0; i < 26; i++){
        glyphs.put(charactersU[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 24, width, height));
    }

    for(int i = 0; i < 10; i++){
        glyphs.put(numbers[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 48, width, height));
    }
}

public Bitmap getBitmap(){
    return bitmap;
}

public void drawString(Canvas canvas, String text, int x, int y){
    if(canvas == null){
        //return a tag
    }
    for(int i = 0; i < text.length(); i++){
        Character ch = text.charAt(i);
        if(glyphs.get(ch) != null){
            canvas.drawBitmap(glyphs.get(ch), x + (i * width), y, null);
        }
    }
}
}

但是,此程序在其他手機上也可以正常工作。 我不確定為什么它會在我的舊POS手機上崩潰。

  1. 檢查您的手機和您的朋友的Android版本。
  2. 您應該找到導致錯誤的代碼行。
  3. 不僅將錯誤消息本身放在這里,而且將整個堆棧放在這里。
  4. 為了將來,請在Eclipse上安裝Android源代碼。 因此,您將確切找到錯誤發生的位置。 它有助於您理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM