0

我正在创建一个需要TableLayout. 我已经完成了我的编码LinearLayout并且它正在工作,但我无法TableLayout在我的项目中使用。我收到错误NullPoinerExceptionin logcat。所以请给我一些解决方案如何TableLayout动态添加。感谢你

public class player_name extends Activity {


LinearLayout player_name;
TableLayout ply_name = new TableLayout(this);
Bundle b,b1;




List<TextView> allEds = new ArrayList<TextView>();
List<Button> allplus = new ArrayList<Button>();
List<Button> allminus = new ArrayList<Button>();
List<EditText> alledit = new ArrayList<EditText>();
List<TextView> alltotal = new ArrayList<TextView>();
//List<TableRow> allrow = new ArrayList<TableRow>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player_name);



    //b = getIntent().getExtras();
    //String resStr = b.getString("name");
    b1 = getIntent().getExtras();
    String[] result = b1.getStringArray("playerName");


    player_name = (LinearLayout) findViewById(R.id.player_name);

    TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams 
            (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);


    TextView[] ed1 = new TextView[result.length+1];
    Button[] plus = new Button[result.length+1];
    Button[] minus = new Button[result.length+1];
    EditText[] point = new EditText[result.length+1];
    TextView[] total = new TextView[result.length+1];
    TableRow[] TR= new TableRow[result.length+1];


    Button btnResult = new Button(player_name.this);
    btnResult.setText(" click here to get RESULT");






    for(int i=0;i<=(result.length-1);i++)
    {

        ed1[i] = new TextView(player_name.this);
        plus[i] = new Button(player_name.this);
        minus[i] = new Button(player_name.this);
        point[i] = new EditText(player_name.this);
        total[i] = new TextView(player_name.this);
        TR[i] = new TableRow(player_name.this);



        allEds.add(ed1[i]);
        alltotal.add(total[i]);
        alledit.add(point[i]);
        allplus.add(plus[i]);
        allminus.add(minus[i]);
        //allrow.add(TR[i]);




        TR[i].addView(ed1[i]);
        TR[i].addView(plus[i]);
        TR[i].addView(minus[i]);
        TR[i].addView(point[i]);
        TR[i].addView(total[i]);

        ply_name.addView(TR[i]);
        TR[i].setLayoutParams(tableRowParams);

        player_name.addView(ply_name);





        ed1[i].setId(i);
        ed1[i].setHeight(50);
        ed1[i].setWidth(300);
        ed1[i].setText(result[i]);
        ed1[i].setTextColor(Color.CYAN);



        total[i].setId(i);
        total[i].setHeight(50);
        total[i].setWidth(300);
        total[i].setText(""+0);
        total[i].setTextColor(Color.CYAN);


        point[i].setId(i);
        point[i].setHeight(50);
        point[i].setWidth(300);
        //point[i].setText(Integer.parseInt("0"));
        point[i].setHint("enter\t"+result[i]+"\'s\tpoint");
        point[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        point[i].setTextColor(Color.BLACK);



        plus[i].setId(i);
        plus[i].setHeight(50);
        plus[i].setWidth(300);
        plus[i].setText("+");
        plus[i].setTextColor(Color.BLACK);



        minus[i].setId(i);
        minus[i].setHeight(50);
        minus[i].setWidth(300);
        minus[i].setText("-");
        minus[i].setTextColor(Color.BLACK);

    }

    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    player_name.addView(btnResult, lp);


    btnResult.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent1 = new Intent(player_name.this,result.class);
            startActivity(intent1);

        }
    });





}

}
4

1 回答 1

-1

把这部分代码:

ply_name = new TableLayout(this);

onCreate()方法中。并定义:

TableLayout ply_name;

作为班级的一员。

于 2013-06-25T03:50:51.147 回答