我有一段代码,它在EditText字段中输入一个数字值,将其解析为Double,然后从另一个Double值(总和)中添加或删除它。这在大多数情况下都能正常工作,除非输入的值(或其添加的值)分别高于或低于1000或-1000。
这里有一些注意事项:用户按下以添加值的按钮会检查它以查看按下按钮时EditText字段是否为空白;如果是这样,它什么都不做,并通过Toast
告诉用户他们需要输入非空值。另一方面,如果用户输入一个值(例如999.99或更小),它将清除EditText字段,将值添加到前面提到的总和,并将输入的值放入ListView适配器。
现在对于奇怪的行为:如果用户输入的值为1000或更高,无论总和等于(例如:500减去1000的总和仍然复制此问题),该数量将添加到ListView但是总和没有改变,EditText保持输入的值(如果从总和中减去值,则在前面附加减号),并通知用户他们需要输入非空值。
通常,单击ListView中的某个项目会将其从列表中删除,并在总和中添加或删除该金额。如果尝试对1000或更高的值进行此操作,应用程序会因NumberFormatException
引用“无效的双重”而崩溃。
我将发布处理以下操作的代码以及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("");
*/
}
LogCat:
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)
答案 0 :(得分:0)
发生错误是因为ParseDouble不知道如何处理逗号(,)。我发现解决此问题的最简单方法是在要预先解析的String上使用ReplaceAll(“,”,“”)。