qxy1547722058
2017-04-22 01:29
采纳率: 100%
浏览 836

Android,求大神看看代码错在哪?一运行就崩溃(づ ●─● )づ

额,手机编写运行的,电脑坏了。。。(ಥ_ಥ)没有日记。
只是个小代码,编着试试(。ò ∀ ó。)
我注释了几乎大部分代码,还是不行。
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)); } }

}

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

4条回答 默认 最新

  • qxy1547722058 2017-04-22 01:35
    已采纳

    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 2017-04-22 01:53

    建议使用debag进行调试,从map数组的创建开始,一步一步的调试,肯定能找到原因的

    打赏 评论
  • qxy1547722058 2017-04-22 02:14

    谢谢各位大神,我知道错在哪里了٩( 'ω' )و

    打赏 评论
  • yourweigrandfather 2017-04-22 08:52

    你的数组应该是10列9行吧哥们儿?是不是这儿错了?

    打赏 评论

相关推荐 更多相似问题