我正在构建一个程序,在运行时创建6个编辑文本,我想将用户在这些edittexts中输入的数据保存到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;
答案 0 :(得分:2)
当您想要访问可能尚未初始化的对象时,会出现“可能尚未初始化”错误。这意味着例如这段代码不起作用:
int a = 5;
EditText et;
if(a == 5) {
et = new EditText();
}
et.setText("some text");
它不起作用,因为et
在if
语句中被初始化,当然这种情况通过了,但编译器不知道。向您展示您在此处犯了错误比最终让您的应用程序在运行时崩溃更安全。
编辑:
您正在EditText
的{{1}}中初始化Button
(因此,当点击onClick()
时,您想要在其他地方访问它(当另一个{时)单击{1}}。编译器会阻止这种情况,因为必须确保EditText已初始化,它不能依赖用户首先单击初始化按钮。
要解决此问题,请将Button
个字段设置为Button
类的字段并在EditText
中初始化它们,Activity
时无需执行此操作单击1}}。就是这样。
答案 1 :(得分:1)
构造如下:
switch (var) {
case 1:
SomeType foo = new SomeType();
// ...
break;
case 2:
foo.something();
// ...
break;
}
存在foo
执行时case 2
未初始化的问题。变量在声明卷曲{}
范围内可见,在这种情况下为switch
。但是,当采用情况2时,代码执行路径不包括变量的初始化。编译器非常聪明,可以注意到这一点并给出警告。
在您的情况下,一个好的解决方案是将EditText
变量声明移动到类成员级别:
private EditText mEd1;
// ...
mEd1 = new EditText(this);
并在引用它们时,检查非空
if (mEd1 != null) {
// ...
String s_time = String.valueOf(mEd1.getText().toString());
答案 2 :(得分:0)
您必须首先查看所有edittext,然后执行您想要的任何内容:
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("");
}
}