刚刚接触android编程,刚开始学习它过去6周,并正在为Android编写扫雷游戏,以及我已经设法做了一些游戏的一部分,没有太多问题。但是,我必须使用TableLayout和TableRow以编程方式设计网格,并在其中插入按钮;所以我写了几行代码来做到这一点,但每当我运行游戏时,我都会收到“确认透视图切换”错误。在android中,我该如何从编程的按钮数组中添加按钮到TableLayout中?
下面是我写的代码 -
` public class Game extends Activity implements OnClickListener {
Button[][] btn = new Button[6][6];
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gamegrid);
int i, j;
LinearLayout layoutVertical = (LinearLayout) findViewById(R.layout.gamegrid);
//create a new TableLayout
TableLayout table = null;
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
LayoutParams param = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for(i = 0; i <6; i++){
table = new TableLayout(this);
table.setWeightSum(5);
layoutVertical.addView(table, param);
for(j=0; j<7; j++){
btn[i][j] = new Button(this);
table.addView(btn[i][j], param);
btn[i][j].setOnClickListener(this);
}
} return;
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
} `
我想我的问题是与以下几行 -
`for(i = 0; i <6; i++){
table = new TableLayout(this);
table.setWeightSum(5);
layoutVertical.addView(table, param);
for(j=0; j<7; j++){
btn[i][j] = new Button(this);
table.addView(btn[i][j], param);
btn[i][j].setOnClickListener(this);
}
}`
它想创建按钮,然后将它们存储在按钮的数组然后将按钮插入TableLayout!
为什么我会收到上述错误?
你能帮我指出做错了什么吗?因为我没有任何错误显示。
感谢
非常感谢我尝试过,它工作正常!但是,如果程序有一个不寻常的问题,它运行良好,但是当它想要执行网格活动时,没有任何反应让我留下空白屏幕!任何想法为什么这可能是? [抱歉没有发布完整程序的代码] – Ange 2012-04-17 20:04:26
你有任何错误或有任何相关的logcat? – zapl 2012-04-17 20:38:29
不幸的是,没有什么出现在logcat – Ange 2012-04-17 20:55:24