2012-08-31 122 views
0

我有一個非常奇怪的錯誤。它只發生在模擬器中。我在多款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(); 
} 
+0

你能提供錯誤信息嗎? –

+0

請提供logcat信息。 – rahul

回答

1

既然你沒有提供的錯誤信息,我走在黑暗中拍攝,並假設它會OOM。

你說運行了幾次(當我= 8)後停止,我相信你沒有釋放資源。位圖有時會佔用大量空間,如果你將它們保存在內存中,如果設備出去OutOfMemory,我不會感到驚訝。不同的設備對內存有不同的規格,經過幾次運行後,內存就被填滿了。

因此,我的建議是清除位圖,使用mBitmap.recycle()和您用於臨時目的的其他存儲。

另外,看看this question

相關問題