2014-01-27 31 views
0

我想顯示tilemap的,但是當我跑我的應用程序,沒有什麼在屏幕上:C的Android tilemap的錯誤

public class MainActivity extends Activity { 

LinearLayout tileMap; 
int width, height = 1; 

int[][] map = { 
     {0,0,0,0,0}, 
     {0,0,0,0,0}, 
     {0,0,0,0,0}, 
     {0,0,0,0,0}, 
     {0,0,0,0,0}}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    tileMap = new LinearLayout(this); 
    generate(tileMap, this); 
    setContentView(tileMap); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

void generate(LinearLayout tileMap, Context context) { 

    for(int y = 0; y < height; y++) { 
     for(int x = 0 ; x < width; x++) { 
      ImageView tile = new ImageView(context); 
      tile.setImageResource(R.drawable.wall); 
      tileMap.addView(tile); 
     } 
    } 
} 
} 

你能告訴我什麼是錯我的代碼? 我很確定這個問題在生成函數裏面......我已經試過去做一個ImageView數組,但它也沒有工作! 謝謝:)

回答

0

的問題是在這裏:

int width, height = 1; 

你認爲寬度已經被初始化爲1,但不是。事實上,它並沒有被初始化,爲int默認值爲0

因此for(int x = 0 ; x < width; x++) {因爲x < width將永遠運行不會求到true

所以初始化兩個:

int width = 1; 
int height = 1;