我有一個非常奇怪的錯誤。它只發生在模擬器中。我在多款Android手機和Acer平板電腦上測試過它,它在那裏效果不錯。Android應用程序崩潰,我不知道爲什麼
我的程序有一個循環,在位圖中加載到Bitmap[] bitCards
。陣列由bitCards = new Bitmap[14]
設置爲14個元素。
現在,循環12次把一個位圖到數組如下:
bitCards[i] = BitmapFactory.decodeStream(inputStream);
,在i = 8,崩潰於本聲明。
如果我
bitCards[0] = BitmapFactory.decodeStream(inputStream);
更換它不會崩潰,我想也許不知何故沒等我,讓下面的
bitCards[8]=BitmapFactory.decodeStream(inputStream); // Still did not crash.
唯一的感覺是數組不夠大當我有
bitCards[i] = BitmapFactory.decodeStream(inputStream);
它是舊的記憶和放入一個新的對象,因此只有我創建一個對象的mory,但是......異常不會消失,我不應該得到某種錯誤嗎?
這裏是我的全碼:
void Shuffle()
{
Random generator;
generator = new Random();
int[] iCardsUsed;
iCardsUsed = new int[55];
for(int i=0;i<55;i++)
iCardsUsed[i]=0;
try {
bitCards = new Bitmap[14];
iCards = new int[14];
iTurnOver = new int[14];
for (int i = 0; i < 12; i++)
{
iTurnOver[i]=0;
int cardId;
do {
cardId = generator.nextInt(50);
} while(iCardsUsed[cardId] ==1);
iCardsUsed[cardId] =1;
iCards[i]=cardId;
iCards[i]=i;
String fName=new String("card");
fName+=Integer.toString(iCards[i]+1);
fName+=".jpg";
AssetManager assetManager= ctx.getAssets();
InputStream inputStream;
inputStream = assetManager.open(fName);
// this is where it crashes
bitCards[i]=BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
} catch(IOException e)
{
gi++;
}
// update screen
invalidate();
}
你能提供錯誤信息嗎? –
請提供logcat信息。 – rahul