如何将运行时动态创建的edittext中的数据保存到sqlite数据库



我正在构建一个程序,该程序在运行时创建6个编辑文本,我想将用户在这些编辑文本中输入的数据保存到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;

当您想要访问对象时,会出现"可能尚未初始化"错误。。。可能尚未初始化。这意味着,例如,这个代码将不起作用:

int a = 5;
EditText et;
if(a == 5) {
et = new EditText();
}
et.setText("some text");

它不会起作用,因为et是在if语句中初始化的,当然在这种情况下会传递这个条件,但编译器不知道。向您表明您在这里犯了一个错误比最终在运行时让应用程序崩溃更安全。

编辑:

您正在ButtononClick()中初始化EditText(因此,当单击Button时),并且您想在其他地方访问它(当单击另一个Button时)。编译器防止了这种情况,因为它必须确保EditText已初始化,不能依赖用户先单击初始化按钮。

为了解决这个问题,创建那些EditText作为Activity类的字段,并在onCreate()中初始化它们,当单击Button时不需要这样做。就是这样。

构造如下:

switch (var) {
case 1:
SomeType foo = new SomeType();
// ...
break;
case 2:
foo.something();
// ...
break;
}

存在CCD_ 13执行时CCD_ 12未初始化的问题。变量在其声明的卷曲{}作用域中可见,即本例中的switch。然而,当采用情况2时,代码执行路径不包括变量的初始化。编译器足够聪明,可以注意到这一点,并向您发出警告。

在您的情况下,一个好的解决方案是将EditText变量声明移动到类成员级别:

private EditText mEd1;
// ...
mEd1 = new EditText(this);

当引用它们时,检查是否为非空

if (mEd1 != null) {
// ...
String s_time = String.valueOf(mEd1.getText().toString());

您必须首先获得所有编辑文本的视图,然后执行任何您想要的操作,如以下所示:

ViewGroup group = (ViewGroup)findViewById(R.id.your_layout);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}

最新更新