当edittext为null并按下添加按钮时,我的代码崩溃...我尝试将整数从edittext复制到数组...我怎么能修复它这个错误?我设置了edittext
机器人:的inputType = “numberDecimal”
我可以试试看吗?抓住?
public static ArrayList<Integer> pulseslist = new ArrayList<Integer>();
public int pulses1[]=null;
private OnClickListener btnAddListener = new OnClickListener() {
public void onClick(View v) {
String ag=editIRpulse1.getText().toString().trim();
if (ag!=null){
int intag= Integer.parseInt(ag);
if(ag.length() > 0){
pulseslist.add(intag);
editIRpulse1.setText(""); // adds text to arraylist and make edittext blank again
}
pulses1 = new int[pulseslist.size()];
for (int i = 0; i < pulseslist.size(); i++) {
pulses1[i] = pulseslist.get(i);
}
}
}
};
答案 0 :(得分:0)
更改此
android:inputType="numberDecimal"
到
android:inputType="number"
因为您只想要int
s。您还应该使用try/catch
来捕获NumberFormatException
以防止空Strings
或无效的数字。如果你遇到异常,就什么都不做。您应该向用户显示一条消息,表明他们已经使用了无效字符
答案 1 :(得分:0)
试试这个
public static ArrayList<Integer> pulseslist = new ArrayList<Integer>();
public int pulses1[]=null;
private OnClickListener btnAddListener = new OnClickListener() {
public void onClick(View v) {
String ag=editIRpulse1.getText().toString();
if (ag!=null){
ag.trim(); //Maybe crash for this method put here........................
int intag= Integer.parseInt(ag);
if(ag.length() > 0){
pulseslist.add(intag);
editIRpulse1.setText(""); // adds text to arraylist and make edittext blank again
}
pulses1 = new int[pulseslist.size()];
for (int i = 0; i < pulseslist.size(); i++) {
pulses1[i] = pulseslist.get(i);
}
}
}
};