2013-06-25 243 views
0

我正在創建一個需要TableLayout的應用程序。我已經完成了我的代碼LinearLayout,它正在工作,但我無法在我的項目中使用TableLayout。我在logcat中收到NullPoinerException的錯誤。所以請給我一些解決方案如何動態地添加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); 

     } 
    }); 





} 

} 
+0

只需在帖子中發佈logcat跟蹤! – 7bluephoenix

+0

你使用什麼代碼作爲'TableLayout'。併發布您的LogCat。 –

回答

-1

放的這部分代碼:在onCreate()方法

ply_name = new TableLayout(this); 

。並且定義如下:

TableLayout ply_name; 

作爲類的成員。

+0

非常感謝你。它現在完成了。謝謝 –

+0

@WilsonS如果它正在工作,那麼它將被你標記爲接受的答案! :) – 7bluephoenix