【问题标题】:Error when I put decimal numbers android studio当我输入十进制数字时出错android studio
【发布时间】:2018-02-09 21:36:44
【问题描述】:

我正在我的 android 应用程序中使用 paypal 实现捐赠按钮。一切都很好,直到我输入带小数的数字时出现错误,但是当我输入整数时,我没有。有谁知道如何做到这一点?我在互联网上搜索了它,但我找不到任何东西

错误:

02-09 22:35:29.256 30695-30695/dev.mros.espanyaapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 java.lang.NumberFormatException: Invalid long: "0,12"
                                                                     at java.lang.Long.invalidLong(Long.java:125)
                                                                     at java.lang.Long.parse(Long.java:362)
                                                                     at java.lang.Long.parseLong(Long.java:353)
                                                                     at java.lang.Long.parseLong(Long.java:319)
                                                                     at java.math.BigDecimal.<init>(BigDecimal.java:344)
                                                                     at java.math.BigDecimal.<init>(BigDecimal.java:425)
                                                                     at dev.mros.espanyaapp.Paypal.getPayment(Paypal.java:72)
                                                                     at dev.mros.espanyaapp.Paypal.onClick(Paypal.java:49)
                                                                     at android.view.View.performClick(View.java:4475)
                                                                     at android.view.View$PerformClick.run(View.java:18786)
                                                                     at android.os.Handler.handleCallback(Handler.java:730)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                     at android.os.Looper.loop(Looper.java:176)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:525)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                     at dalvik.system.NativeStart.main(Native Method)

代码:

public class Paypal extends AppCompatActivity implements View.OnClickListener {

//The views
private Button buttonPay;
private EditText editTextAmount;

//Payment Amount
private String paymentAmount;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paypal);
    buttonPay = (Button) findViewById(R.id.buttonPay);

    editTextAmount = (EditText) findViewById(R.id.editTextAmount);
    buttonPay.setOnClickListener(this);
    Intent intent = new Intent(this, PayPalService.class);
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
    startService(intent);

}

@Override
public void onClick(View view) {
    getPayment();
}
//Paypal intent request code to track onActivityResult method
public static final int PAYPAL_REQUEST_CODE = 123;


//Paypal Configuration Object
private static PayPalConfiguration config = new PayPalConfiguration()
        // Start with mock environment.  When ready, switch to sandbox (ENVIRONMENT_SANDBOX)
        // or live (ENVIRONMENT_PRODUCTION)
        .environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
        .clientId(PayPalConfig.PAYPAL_CLIENT_ID);

@Override
public void onDestroy() {
    stopService(new Intent(this, PayPalService.class));
    super.onDestroy();
}
private void getPayment() {
    //Getting the amount from editText
    paymentAmount = editTextAmount.getText().toString();

    //Creating a paypalpayment
    PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Simplified Coding Fee",
            PayPalPayment.PAYMENT_INTENT_SALE);

    //Creating Paypal Payment activity intent
    Intent intent = new Intent(this, PaymentActivity.class);

    //putting the paypal configuration to the intent
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

    //Puting paypal payment to the intent
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);

    //Starting the intent activity for result
    //the request code will be used on the method onActivityResult
    startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //If the result is from paypal
    if (requestCode == PAYPAL_REQUEST_CODE) {

        //If the result is OK i.e. user has not canceled the payment
        if (resultCode == Activity.RESULT_OK) {
            //Getting the payment confirmation
            PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);

            //if confirmation is not null
            if (confirm != null) {
                try {
                    //Getting the payment details
                    String paymentDetails = confirm.toJSONObject().toString(4);
                    Log.i("paymentExample", paymentDetails);

                    //Starting a new activity for the payment details and also putting the payment details with intent
                    startActivity(new Intent(this, ConfirmationActivity.class)
                            .putExtra("PaymentDetails", paymentDetails)
                            .putExtra("PaymentAmount", paymentAmount));

                } catch (JSONException e) {
                    Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.i("paymentExample", "The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

}

【问题讨论】:

  • 0,12?为什么有一个,而不是一个。?
  • 逗号不是小数。您可以清楚地看到错误是您阅读了消息
  • 但是,0.12 是双精度数,而不是长数。
  • 我该如何实现呢?
  • @Code-Apprentice 只是在询问如何修复代码时真正查看了代码。 String.valueOf(paymentAmount).. 你的答案可能是对的。他传入 a ,而不是 a 。通过外星人

标签: android database paypal


【解决方案1】:

在美国,我们使用. 作为小数点,而不是像某些国家/地区使用的,。最简单的解决方法是要求用户输入数字为0.12,例如,而不是0,12。如果要允许, 输入十进制数字,则需要使用LocaleNumberFormat 来解析来自EditText 的输入。

【讨论】:

  • 怎么会改变?
  • @zzxbx 你应该阅读localesNumberFormat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多