qxy1547722058
qxy1547722058
2017-04-22 01:02
采纳率: 100%
浏览 930
已采纳

程序运行就崩溃。。布局文件就一个tablelayout

package com.sptao.chess;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.util.*;
import android.content.*;

public class MainActivity extends Activity
{
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

private final int FP = ViewGroup.LayoutParams.FILL_PARENT;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int map[][]={{0,0,0,0,0,0,0,0,0,0},//0行
    {1,0,0,0,0,0,0,0,0,0},//1
    {0,0,0,0,0,0,0,0,0,0},//2
    {0,0,0,0,1,0,0,0,0,0},//3
    {0,0,0,0,0,0,0,0,0,0},//4
    {0,0,0,0,0,0,1,0,0,0},//5
    {1,0,0,0,0,0,0,0,0,0},//6
    {0,0,0,0,0,0,0,0,0,0},//7
    {0,0,0,0,0,0,0,0,0,0}};//8.9
    int a = 0;
    Button bu []=new Button[100];
    //新建TableLayout的实例   
    TableLayout tableLayout = (TableLayout)findViewById(R.id.mainTableLayout1);  
    //全部列自动填充空白处   
    tableLayout.setShrinkAllColumns(true);  
    //生成10行,9列的表格   
    for(int row=0;row<10;row++)  //行
    {  
        TableRow tableRow=new TableRow(this);  
        for(int col=0;col<9;col++)  //列
        {  
        a++;
        switch( map[row][col])
        {
        case 0:

                break;
        default:

                bu.setTag(col+row);
                bu[a].setText("("+col+","+row"));
                tableRow.addView(bu[a]);
}


        //新建的TableRow添加到TableLayout   
        tableLayout.addView(tableRow, new TableLayout.LayoutParams(WC, WC));  
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • ljheee
    ljheee 2017-04-22 01:20
    已采纳

    运行就崩溃,打印的日志呢...........

    点赞 评论
  • qxy1547722058
    qxy1547722058 2017-04-22 01:30

    额,手机编写运行的,电脑坏了。。。(ಥ_ಥ)没有日记。
    只是个小代码,编着试试(。ò ∀ ó。)
    我注释了几乎大部分代码,还是不行。
    package com.sptao.chess;

    import android.app.*;
    import android.os.*;
    import android.widget.*;
    import android.view.*;
    import android.util.*;
    import android.content.*;

    public class MainActivity extends Activity
    {
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

    private final int FP = ViewGroup.LayoutParams.FILL_PARENT;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int map[][]={{0,0,0,0,0,0,0,0,0,0},//0行
        {1,0,0,0,0,0,0,0,0,0},//1
        {0,0,0,0,0,0,0,0,0,0},//2
        {0,0,0,0,1,0,0,0,0,0},//3
        {0,0,0,0,0,0,0,0,0,0},//4
        {0,0,0,0,0,0,1,0,0,0},//5
        {1,0,0,0,0,0,0,0,0,0},//6
        {0,0,0,0,0,0,0,0,0,0},//7
        {0,0,0,0,0,0,0,0,0,0}};//8.9
        int a = 0;
        Button bu []=new Button[100];
        //新建TableLayout01的实例   
        TableLayout tableLayout = (TableLayout)findViewById(R.id.mainTableLayout1);  
        //全部列自动填充空白处   
        tableLayout.setShrinkAllColumns(true);  
        //生成10行,9列的表格   
        for(int row=0;row<10;row++)  //行
        {  
            TableRow tableRow=new TableRow(this);  
            for(int col=0;col<9;col++)  //列
            {  
            a++;
            //switch( map[row][col])
            //{
            //case 0:
    
                    //break;
            //default:
    
                    //bu.setTag(col+row);
                    //bu[a].setText("("+col+","+row+")");
                    //TableLayout.LayoutParams btParams = new TableLayout.LayoutParams (width/10,height/10);  //设置按钮的宽度和高度
                    //btParams.leftMargin = 1;   //横坐标定位        
                    //btParams.topMargin = 2;   //纵坐标定位       
                    //tableRow.addView(bu,btParams);   //将按钮放入layout组件
                    tableRow.addView(bu[a]);
                //break;
            //}
    
                //批量设置监听
                //for (int k = 0; k <= bu.length-1; k++) { 
                    //这里不需要findId,因为创建的时候已经确定哪个按钮对应哪个Id
                    //bu[k].setTag(k);                //为按钮设置一个标记,来确认是按下了哪一个按钮
                    //bu[k].setOnClickListener(new Button.OnClickListener() {
                            //@Override
                            //public void onClick(View v) {
                                //int i = (Integer) v.getTag();   
                                //Intent intent = new Intent();
                                //intent.setClass(MainActivity.this, activityA.class);
                                //Bundle bundle = new Bundle();
                                //bundle.putInt("count", i);
                                //intent.putExtras(bundle);
                                //startActivity(intent);
                                //MainActivity.this.finish();                   
                            //}
                        //});
                //}
    
                }
    
    
            //新建的TableRow添加到TableLayout   
            tableLayout.addView(tableRow, new TableLayout.LayoutParams(WC, WC));  
        }
    }
    

    }

    点赞 评论
  • MyStephenCurry
    MyStephenCurry 2017-04-22 01:51

    可能是button你实例化的时候的问题吧

    点赞 评论
  • gaiew
    gaiew 2017-04-22 01:54

    1234112312313131313

    点赞 评论

相关推荐