1

我正在尝试在我的应用程序中执行“使用 PayPal 付款”按钮。

这是我单击此按钮时的代码:

class PaypalTask extends AsyncTask<Void, Void, Void> {
        protected final char[] TOTAL_GBP=null;

        public Void doInBackground(Void... arg0) {
            ppObject = PayPal.initWithAppID(getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            btnPaypal = ppObject.getCheckoutButton(getApplicationContext(), PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            btnPaypal.setLayoutParams(params);
            rlPay.addView(btnPaypal);
            if(utils.prefs.getBoolean("isLogged", false)) btnPaypal.setVisibility(View.VISIBLE); else btnPaypal.setVisibility(View.GONE);

            btnPaypal.setOnClickListener(new OnClickListener(){
                public void onClick(View view) {

                    // to check whether there are any books in the shopping cart
                    if(!cid.isEmpty()){
                        PayPalPayment payment = new PayPalPayment();
                        payment.setSubtotal(new BigDecimal(TOTAL_GBP));
                        payment.setCurrencyType("GBP");
                        payment.setRecipient(Utils.PAYPAL_ACCOUNT);
                        payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
                        Intent checkoutIntent = PayPal.getInstance().checkout(payment, getBaseContext());
                        startActivityForResult(checkoutIntent, 1);
                    }else{
                        utils.showToast("Your shopping cart is empty.");
                        // The PayPal button needs to be updated so that the user can click on it again.
                        btnPaypal.updateButton();
                    }
                }   
            });
        }
    }

我在 Log Cat 中收到以下错误:

03-23 11:56:33.539: E/AndroidRuntime(739): FATAL EXCEPTION: main
03-23 11:56:33.539: E/AndroidRuntime(739): java.lang.NullPointerException
03-23 11:56:33.539: E/AndroidRuntime(739):  at java.math.BigDecimal.<init>(BigDecimal.java:406)
03-23 11:56:33.539: E/AndroidRuntime(739):  at com.fokrul.justdeals.ActivityTab$PaypalTask$1.onClick(ActivityTab.java:1587)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.view.View.performClick(View.java:3110)
03-23 11:56:33.539: E/AndroidRuntime(739):  at com.paypal.android.MEP.CheckoutButton.onClick(Unknown Source)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.view.View.performClick(View.java:3110)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.view.View$PerformClick.run(View.java:11934)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.os.Handler.handleCallback(Handler.java:587)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.os.Looper.loop(Looper.java:132)
03-23 11:56:33.539: E/AndroidRuntime(739):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-23 11:56:33.539: E/AndroidRuntime(739):  at java.lang.reflect.Method.invokeNative(Native Method)
03-23 11:56:33.539: E/AndroidRuntime(739):  at java.lang.reflect.Method.invoke(Method.java:491)
03-23 11:56:33.539: E/AndroidRuntime(739):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-23 11:56:33.539: E/AndroidRuntime(739):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-23 11:56:33.539: E/AndroidRuntime(739):  at dalvik.system.NativeStart.main(Native Method)

我发现这是因为我可能为 TOTAL_GBP 分配了 NULL 值。

如果我将其更改为:

payment.setSubtotal(new BigDecimal("8.25"));

它有效,但显示为 8.25 英镑作为结帐价值。但我希望它从 TOTAL_GBP 定义的购物车小计中获得价值。当我使用 TOTAL_GBP 而不是“8.25”或任何双浮点值时,它只会崩溃。

有什么想法吗?

4

0 回答 0