2014-02-23 95 views
0

我試圖從可繪製的陣列編程方式設置的ImageView的形象,而且不斷得到一個NullPointerException ....確實這個方法查找設置圖像資源的正確..設置ImageView的繪製對象programmically

//get a random drawable 
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1}; 
Random whichImage = new Random(); 
int theImage = whichImage.nextInt(imageSelection.length); 

displayImage.setBackgroundResource(theImage); 
+1

你在哪裏以及如何初始化displayImage? – BrainCrash

回答

3

您正在將圖像資源設置爲隨機數。 你需要這樣做:

int theImage = imageSelection[whichImage.nextInt(imageSelection.length)]; 
displayImage.setBackgroundResource(theImage); 
+0

完美,歡呼聲 – dancingbush

+0

祝你好運:) –

相關問題