Android - 当没有在 edittext 中输入任何内容时出现空指针异常
Android - null pointer exception when nothing is typed in edittext
我有一个 edittext,我在数量编辑框中输入 products.Clearing 所有数据的数量,导致设备 crash.I 使用addTextChangedListener(new TextWatcher()
代码
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String s1=pricetxt.getText().toString();
String s2=edittext.getText().toString();
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('"+s2+"')");
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int res=i1*i2;
totltxt.setText(Integer.toString(res));
if (s2.matches("")) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
错误这里写的很清楚
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
这意味着您将 s1
、s2
的空字符串值解析为 int
。所以在解析它之前只需检查
if(!s1.equals("")) {
int i1=Integer.parseInt(s1);
}
if(!s2.equals("")) {
int i2=Integer.parseInt(s2);
}
您遇到此崩溃是因为 s1 或 s2 可能为空或 null,因此您必须检查空字符串或 null 字符串。
将您的代码更改为:
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String s1 = pricetxt.getText().toString();
String s2 = edittext.getText().toString();
SQLiteDatabase mydb = cntxt.openOrCreateDatabase("addcart",
Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('" + s2 + "')");
int i1 = 0;
if (!s1.equals("")) {
i1 = Integer.parseInt(s1);
}
int i2 = 0;
if (!s2.equals("")) {
i2 = Integer.parseInt(s2);
}
int res = i1 * i2;
totltxt.setText(Integer.toString(res));
if (s2.matches("")) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
我觉得你做错了
使用文本更改侦听器时,您从 CharSequence 获取编辑文本的值
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String s1 = pricetxt.getText().toString();
String s2 = s.toString();
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('"+s2+"')");
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int res=i1*i2;
totltxt.setText(Integer.toString(res));
if (s.equals("")||s==null) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
我有一个 edittext,我在数量编辑框中输入 products.Clearing 所有数据的数量,导致设备 crash.I 使用addTextChangedListener(new TextWatcher()
代码
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String s1=pricetxt.getText().toString();
String s2=edittext.getText().toString();
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('"+s2+"')");
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int res=i1*i2;
totltxt.setText(Integer.toString(res));
if (s2.matches("")) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
错误这里写的很清楚
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
这意味着您将 s1
、s2
的空字符串值解析为 int
。所以在解析它之前只需检查
if(!s1.equals("")) {
int i1=Integer.parseInt(s1);
}
if(!s2.equals("")) {
int i2=Integer.parseInt(s2);
}
您遇到此崩溃是因为 s1 或 s2 可能为空或 null,因此您必须检查空字符串或 null 字符串。
将您的代码更改为:
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String s1 = pricetxt.getText().toString();
String s2 = edittext.getText().toString();
SQLiteDatabase mydb = cntxt.openOrCreateDatabase("addcart",
Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('" + s2 + "')");
int i1 = 0;
if (!s1.equals("")) {
i1 = Integer.parseInt(s1);
}
int i2 = 0;
if (!s2.equals("")) {
i2 = Integer.parseInt(s2);
}
int res = i1 * i2;
totltxt.setText(Integer.toString(res));
if (s2.matches("")) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
我觉得你做错了
使用文本更改侦听器时,您从 CharSequence 获取编辑文本的值
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String s1 = pricetxt.getText().toString();
String s2 = s.toString();
SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO add2cart (pqty)VALUES('"+s2+"')");
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int res=i1*i2;
totltxt.setText(Integer.toString(res));
if (s.equals("")||s==null) {
edittext.setText("");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});