0

当edittext为空并按添加按钮时,我的代码崩溃...我尝试将整数从edittext复制到数组...我该如何解决这个错误?我已经设置了edittext
android: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);
            }

            }
        }

    };
4

2 回答 2

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);
            }

            }
        }

    };
于 2013-05-21T20:34:04.500 回答
0

改变这个

android:inputType="numberDecimal"

android:inputType="number"

因为你只想要ints。您还应该使用 atry/catch来捕获 aNumberFormatException以防止为空Strings或无效的数字。如果您发现异常,请不要什么都不做。您应该向用户显示他们输入了无效字符的消息

于 2013-05-21T20:24:45.250 回答