gpt4 book ai didi

android - 当我输入十进制数 android studio 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:19 25 4
gpt4 key购买 nike

我正在我的 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。如果要允许 , 以十进制数字输入,则需要使用 LocaleNumberFormat 来解析来自 的输入>编辑文本

关于android - 当我输入十进制数 android studio 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48714449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com