2012-03-29 53 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源代碼。所以你會準確地找到錯誤發生的地方。它有助於理解。
相關問題