1

每当我在不输入价格的情况下提交详细信息时,它都会显示错误,不幸的是您的项目已停止工作,例如价格在数据库中具有整数类型。除了价格,所有其他领域都工作正常。

任何人请帮我解决........

     <EditText android:id="@+id/txtprice"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:numeric="decimal|integer"/>

.java

private void adddetails() {

    // TODO Auto-generated method stub
    String catname=spinner1.getSelectedItem().toString();
    String member=txtname.getText().toString();
    int price=Integer.parseInt(txtprice.getText().toString());
    String price1=txtprice.getText().toString();
    String desc=txtdesc.getText().toString();
    String date=txtdate.getText().toString();

    validation();

    if(txtname.getText().toString().length() == 0||txtdesc.getText().toString().length() == 0|| txtprice.getText().toString().length() == 0||price < 0 || price1==""){
        Toast.makeText(getApplicationContext(), "Enter all details",
                  Toast.LENGTH_SHORT).show();
    }
    else{


    long id = dbAdapter.add(catname, member, price, desc, date);
    if (id > 0) {
        Toast.makeText(getApplicationContext(), "Details has addded successfully",
                  Toast.LENGTH_SHORT).show();
        txtname.setText("");
        txtprice.setText("");
        txtdesc.setText("");
        txtdate.setText("");

        //categoryList.set(0, "");
        System.out.println(" categoryList.get(0) ****** " + categoryList.get(0));
        categoryList.set(0,  categoryList.get(0));      
        dataAdapter.notifyDataSetChanged();



    } else {
        Toast.makeText(getApplicationContext(), "Failed to add new details",
                  Toast.LENGTH_SHORT).show();
    }
    }

}


private void validation() {
    // TODO Auto-generated method stub
    int price=Integer.parseInt(txtprice.getText().toString());
    String price1=txtprice.getText().toString();
    if( txtname.getText().toString().length() == 0 )
        txtname.setError( "Member name is required!" );
    if( txtdesc.getText().toString().length() == 0 )
        txtdesc.setError( "Description is requied!" );
    if( txtdate.getText().toString().length() == 0 )
        txtdate.setError( "select date is required!" );     
    *if( txtprice.getText().toString().length() == 0 || price1.matches("^[0-9]$")==false )
        txtprice.setError( "Expense amount is requied!" );*
    if( price < 0 || price1==""){
        Toast.makeText(getApplicationContext(), "price field ",
                  Toast.LENGTH_SHORT).show();

    }
}
4

1 回答 1

0

你的 xml 有:

android:numeric="decimal|integer"

然后你的代码有:

int price = Integer.parseInt(txtprice.getText().toString());

这意味着您的 EditText 字段允许十进制数字,这在尝试解析为整数时会引发错误。

如果数据库中的价格字段是整数类型,则需要将其更改为某种浮点值类型,因为整数只是整数,不适合存储价格(例如 1.25 美元)。此外,您应该将您的 xml 更改为:

android:numeric="decimal"

和你的代码:

double price = Double.parseDouble(txtPrice.getText().toString());
于 2014-01-24T00:42:54.443 回答