我有一段代码,它接受在 EditText 字段中输入的数字值,将其解析为 Double,然后从另一个 Double 值(总和)中添加或删除它。这在大多数情况下都可以正常工作,除非输入的值(或添加的值)分别高于或低于 1000 或 -1000。
这里有一些注意事项:用户按下添加值的按钮会检查按钮按下时 EditText 字段是否为空白;如果是这样,它什么也不做,并通过 a 告诉用户Toast
他们需要输入一个非空白值。另一方面,如果用户输入一个值(比如 999.99 或更小),它将清除 EditText 字段,将该值添加到前面提到的 sum 中,并将输入的值放入 ListView 适配器。
现在对于奇怪的行为:如果用户输入一个 1000 或更大的值,无论总和是多少(例如:500 减去 1000 的总和仍然会复制此问题),该金额会添加到 ListView,但sum 不变,EditText 保留输入的值(如果从总和中减去该值,则在前面附加一个减号),并通知用户他们需要输入一个非空白值。
通常,单击 ListView 中的项目会将其从列表中删除,并将该金额添加到总和或从总和中删除。如果尝试对 1000 或更大的值进行此操作,则应用程序崩溃并NumberFormatException
引用“无效的 Double”。
我将在下面发布处理上述操作的代码以及 LogCat。如果有人可以帮助我解决这个问题,我将不胜感激,因为这是阻止我向公众发布我的应用程序的唯一原因。谢谢!
/**
* Method to handle ListView clicks. It should ask the user if they want to
* remove the clicked item, and on confirmation, should do so.
*/
@Override
protected void onListItemClick(ListView l, View v, final int position, long id){
final String item = (String) getListAdapter().getItem(position).toString();
final TextView allowance = (TextView) findViewById(R.id.main_textview_allowance);
// Offer up a dialog window to ask if the user really wants to delete
// the clicked item.
AlertDialog.Builder deleteDialog = new AlertDialog.Builder(this);
deleteDialog.setTitle("Confirmation");
deleteDialog.setMessage("Are you sure you want to delete " + item + "?");
deleteDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1){
// Delete the item
adapter.remove(item);
adapter.notifyDataSetChanged();
// We need to remove the deleted value from our allowance
double subtraction = Double.parseDouble(item.toString());
double newValue = Double.parseDouble(allowance.getText().toString()) - subtraction;
allowance.setText(moneyFormat.format(newValue));
Toast.makeText(MainActivity.this, item + " transaction deleted.", Toast.LENGTH_LONG).show();
}
});
deleteDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1){
// Do nothing
}
});
deleteDialog.show();
}// onListItemClick
...
/**
* Add an expense to the ListView
* @param view The calling View object.
*/
public void addExpense(View view){
EditText expense = (EditText)findViewById(R.id.main_edittext_expenseentry);
TextView allowance = (TextView) findViewById(R.id.main_textview_allowance);
// Invert the value given such that it subtracts the expense from the allowance,
// not add to it. But first, if the field is blank, don't do anything and let
// the user know they need to input a value first.
try{
double expenseValue = Double.parseDouble(expense.getText().toString()) * -1;
expense.setText(moneyFormat.format(expenseValue));
adapter.add(expense.getText().toString());
adapter.notifyDataSetChanged();
// We need to subtract the new value from our daily allowance
double subtraction = Double.parseDouble(expense.getText().toString());
double newValue = (Double.parseDouble(allowance.getText().toString())) + subtraction;
allowance.setText(moneyFormat.format(newValue));
expense.setText("");
}catch(NumberFormatException e){
Toast.makeText(this, "You need to enter a non-blank amount!", Toast.LENGTH_SHORT).show();
}
}
/**
* Add a credit to the ListView
* @param view The calling View object.
*/
public void addCredit(View view){
EditText expense = (EditText)findViewById(R.id.main_edittext_expenseentry);
TextView allowance = (TextView) findViewById(R.id.main_textview_allowance);
// Invert the value given such that it subtracts the expense from the allowance,
// not add to it. But first, if the field is blank, don't do anything and let
// the user know they need to input a value first.
try{
double expenseValue = Double.parseDouble(expense.getText().toString());
expense.setText(moneyFormat.format(expenseValue));
adapter.add(expense.getText().toString());
adapter.notifyDataSetChanged();
// We need to add the new value to our daily allowance
double addition = Double.parseDouble(expense.getText().toString());
double newValue = (Double.parseDouble(allowance.getText().toString())) + addition;
allowance.setText(moneyFormat.format(newValue));
expense.setText("");
}catch(NumberFormatException e){
Toast.makeText(this, "You need to enter a non-blank amount!", Toast.LENGTH_SHORT).show();
}
/** OLD CODE
EditText expense = (EditText)findViewById(R.id.main_edittext_expenseentry);
TextView allowance = (TextView) findViewById(R.id.main_textview_allowance);
expense.setText(moneyFormat.format(Double.parseDouble(expense.getText().toString())));
expense.setTextColor(Color.GREEN);
adapter.add(expense.getText().toString());
adapter.notifyDataSetChanged();
expense.setTextColor(Color.BLACK);
// We need to add the new value from our daily allowance
double addition = Double.parseDouble(expense.getText().toString());
double newValue = (Double.parseDouble(allowance.getText().toString())) + addition;
allowance.setText(moneyFormat.format(newValue));
if(newValue < 0)
allowance.setTextColor(Color.RED);
else
allowance.setTextColor(Color.BLACK);
expense.setText("");
*/
}
日志猫:
03-30 15:51:36.831: E/AndroidRuntime(15713): FATAL EXCEPTION: main
03-30 15:51:36.831: E/AndroidRuntime(15713): java.lang.NumberFormatException: Invalid double: "1,000.00"
03-30 15:51:36.831: E/AndroidRuntime(15713): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-30 15:51:36.831: E/AndroidRuntime(15713): at java.lang.StringToReal.parseDouble(StringToReal.java:269)
03-30 15:51:36.831: E/AndroidRuntime(15713): at java.lang.Double.parseDouble(Double.java:295)
03-30 15:51:36.831: E/AndroidRuntime(15713): at com.argusrho.budgeteer.MainActivity$1.onClick(MainActivity.java:249)
03-30 15:51:36.831: E/AndroidRuntime(15713): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
03-30 15:51:36.831: E/AndroidRuntime(15713): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 15:51:36.831: E/AndroidRuntime(15713): at android.os.Looper.loop(Looper.java:137)
03-30 15:51:36.831: E/AndroidRuntime(15713): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-30 15:51:36.831: E/AndroidRuntime(15713): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 15:51:36.831: E/AndroidRuntime(15713): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 15:51:36.831: E/AndroidRuntime(15713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 15:51:36.831: E/AndroidRuntime(15713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 15:51:36.831: E/AndroidRuntime(15713): at dalvik.system.NativeStart.main(Native Method)