Android - 在edittext中没有输入任何内容时出现空指针异常

时间:2015-01-22 06:40:37

标签: android android-edittext

我有一个edittext,我在其中输入产品数量。在数量的编辑框中清除所有数据,导致设备崩溃。我使用 addTextChangedListener(new TextWatcher()

enter image description here

代码

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

    }
});

3 个答案:

答案 0 :(得分:3)

这里的错误清楚地说明了

 int i1=Integer.parseInt(s1);
 int i2=Integer.parseInt(s2);

表示您将空字符串值s1s2解析为int。所以在解析之前只需检查

 if(!s1.equals("")) {
     int i1=Integer.parseInt(s1);
 }

 if(!s2.equals("")) {
     int i2=Integer.parseInt(s2);
 }

答案 1 :(得分:2)

您遇到此崩溃是因为s1或s2可能为空或为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

            }
        });

答案 2 :(得分:0)

我认为你做错了

使用textchange listener

时,您从CharSequence获取edittext的值
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

            }
        });