所以我熟悉OOP和java基礎知識。一些Android的細微差別仍然逃脫我。Android開發中的範圍問題
我現在有一些真正的問題與我的一些課程的範圍。這裏有一個例子:
在我的活動中,我引用了一個名爲「玩家」的自定義類,玩家可以做很多事情,包括繪製圖塊和放置圖塊。
我在我的活動中保留了AVAILABLE_TILES數組中所有未提取的可用圖塊的列表。這很重要,因爲我在許多不同的情況下一直參考這個列表。
雖然這是我的問題。
下面是我的活動代碼:
public void setUpPlayers(){
for (int i = 0; i < players.length; i++) {
players[i] = new Player(playerNames[i], 6000);
players[i].drawTile(1); //draw one tile
//find the ID for the tile just placed
String tileID = players[i].findTileIdByIndex(0); //first tile in thier tile pool
players.[i].placeTile(tileID);
Log.d(players[i].getName(), tileID);
}
}
,這裏是從「玩家」類的代碼:
public void drawTile(int quantity){
//repeat for the total number of tiles being drawn
for (int i = 0; i < quantity; i++) {
//get a random tile ID from the list of available tiles
int randomTileIndex = RNG.nextInt(AVAILABLE_TILES.size());
String randomTileName = AVAILABLE_TILES.get(randomTileIndex);
//add this tile to the player's pool of tiles
addPlayerTile(randomTileName);
Log.d("AVAILABLE_TILE_REMOVED", String.valueOf(randomTileIndex));
//and remove that tile now from the available tiles list
AVAILABLE_TILES.remove(randomTileIndex);
}
}
因此,大家可以看到,我已經得到了保持一個ArrayList每個可用拼貼的ID。 (不在任何玩家池中,也不在板上)。
問題是,自從那個ArrayList在活動中,我似乎無法在Player.java中引用它。
我是否需要將一些與android相關的庫導入到Player.java中?
在另一種情況下,我創建了一個擴展Button的自定義類「Tile」的數組,並且在引用活動中的全局(靜態)變量時沒有問題。
那麼有什麼解決方案?
預先感謝您的幫助,
JRad
堆棧溢出說我必須等待2天后才能接受。 –