2012-03-29 38 views
0

我正在创建一个基于Rects和字形创建菜单的程序。由于某些原因,这段代码在模拟器和我的朋友电话上工作,但它不适用于我的。我开始相信也许我的手机太老了(作为彗星系列)。然而,就像任何一款优秀的手机应用开发者一样,我希望我的应用能够在任何类型的Android手机上工作,所以我正在深入研究这个问题。我得到一个错误说:在某些设备上运行时错误“x + width必须为<= bitmap.width()”

造成的:java.lang.IllegalArgumentException异常:X +宽度必须< = 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); 
     } 
    } 
} 
} 

但是,这个程序在其他手机上工作得很好。我不确定为什么它会在我的旧手机上崩溃。

回答

0
  1. 查看您手机的Android版本以及朋友的手机版本。
  2. 您应该找到导致错误的代码行。
  3. 把这里不仅错误信息本身,而且整个堆栈。
  4. 以后,在Eclipse上安装Android源代码。所以你会准确地找到错误发生的地方。它有助于理解。
相关问题