我正在構建一個在運行時創建6個編輯文本的程序,並且我想將用戶在這些edittext中輸入的數據保存到sqlite數據庫中,但是出現錯誤。我提供了創建我的編輯文本的代碼,我希望我能得到一些幫助。第二種情況出現錯誤。如何在運行時將動態創建的edittext的數據保存到sqlite數據庫?
the edittext is may have not been initialized.
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttonAddIntervention:
final TableLayout table = (TableLayout)findViewById(R.id.tableinterventions);
EditText ed = new EditText(this);
TextView tv = new TextView(this);
final TableRow tabr = new TableRow(this);
tabr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setText("Code");
ed.setHint("Code");
ed.setText("", null);
ed.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr.addView(tv);
tabr.addView(ed);
table.addView(tabr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed1 = new EditText(this);
TextView tv1 = new TextView(this);
Button b1 = new Button(this);
final TableRow tabr1 = new TableRow(this);
tabr1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setText("-");
tv1.setText("Start Time");
ed1.setHint("Start Time");
ed1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr1.addView(tv1);
tabr1.addView(ed1);
tabr1.addView(b1);
table.addView(tabr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed2 = new EditText(this);
TextView tv2 = new TextView(this);
final TableRow tabr2 = new TableRow(this);
tabr2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setText("End Time");
ed2.setHint("End Time");
ed2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr2.addView(tv2);
tabr2.addView(ed2);
table.addView(tabr2, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed3 = new EditText(this);
TextView tv3 = new TextView(this);
final TableRow tabr3 = new TableRow(this);
tabr3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setText("Description");
ed3.setHint("Description");
ed3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr3.addView(tv3);
tabr3.addView(ed3);
table.addView(tabr3,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed4 = new EditText(this);
TextView tv4 = new TextView(this);
final TableRow tabr4 = new TableRow(this);
tabr4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setText("Product");
ed4.setHint("Product");
ed4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr4.addView(tv4);
tabr4.addView(ed4);
table.addView(tabr4,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed5 = new EditText(this);
TextView tv5 = new TextView(this);
final TableRow tabr5 = new TableRow(this);
tabr5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv5.setText("Serial Number");
ed5.setHint("Serial Number");
ed5.setLayoutParams(new TableRow.LayoutParams(190,LayoutParams.WRAP_CONTENT));
tv5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr5.addView(tv5);
tabr5.addView(ed5);
table.addView(tabr5,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v1) {
// TODO Auto-generated method stub
table.removeView(tabr);
table.removeView(tabr1);
table.removeView(tabr2);
table.removeView(tabr3);
table.removeView(tabr4);
table.removeView(tabr5);
}
});
break;
case R.id.BtnTestSaveAll:
InterventionsDatabase idb1 = new InterventionsDatabase();
//the error is here the ed,ed1,ed2,ed3,ed4 and ed5 may not have been initialized
String cod= String.valueOf(ed.getText());
String s_time = String.valueOf(ed1.getText().toString());
String e_time = String.valueOf(ed2.getText().toString());
String d = String.valueOf(ed3.getText().toString());
String p = String.valueOf(ed4.getText().toString());
String Sn = String.valueOf(ed5.getText().toString());
idb1.setEmp_code(Integer.parseInt(cod));
idb1.setEmp_startTime(Integer.parseInt(s_time));
idb1.setEmp_endTime(Integer.parseInt(e_time));
idb1.setDescription(d);
idb1.setProduct(p);
idb1.setSerialNumber(Integer.parseInt(Sn));
Dbhelper.createInterventionsDB(Integer.parseInt(cod),Integer.parseInt(s_time),Integer.parseInt(e_time), d, p,Integer.parseInt(Sn));
Toast.makeText(getApplicationContext(), "Yeah", Toast.LENGTH_SHORT).show();
break;
請張貼相關的代碼和你得到的錯誤。 – laalto
請發表您的代碼 – Ameer
我該如何在這裏發佈代碼? –