0

我试图让 PayPal 按钮在我的 Android 应用程序的结帐活动中工作。但我得到一个空指针异常,它指的是我的代码中的第 1585 行。logcat 输出如下:

03-22 21:27:28.659: W/dalvikvm(419): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-22 21:27:28.669: E/AndroidRuntime(419): FATAL EXCEPTION: main
03-22 21:27:28.669: E/AndroidRuntime(419): java.lang.NullPointerException
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.math.BigDecimal.<init>(BigDecimal.java:406)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.fokrul.justdeals.ActivityTab$PaypalTask$1.onClick(ActivityTab.java:1585)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View.performClick(View.java:3110)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.paypal.android.MEP.CheckoutButton.onClick(Unknown Source)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View.performClick(View.java:3110)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.view.View$PerformClick.run(View.java:11934)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Handler.handleCallback(Handler.java:587)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.os.Looper.loop(Looper.java:132)
03-22 21:27:28.669: E/AndroidRuntime(419):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.lang.reflect.Method.invokeNative(Native Method)
03-22 21:27:28.669: E/AndroidRuntime(419):  at java.lang.reflect.Method.invoke(Method.java:491)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-22 21:27:28.669: E/AndroidRuntime(419):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-22 21:27:28.669: E/AndroidRuntime(419):  at dalvik.system.NativeStart.main(Native Method)

我的 PayPal 活动的 Onclick 方法代码如下:

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();
            <<LINE 1585-->>>>> payment.setSubtotal(new BigDecimal(TOTAL_GBP));
                        payment.setCurrencyType("USD");
                        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();
                    }
                }   
            });
        }
    }

有什么想法可能会出错吗?

4

1 回答 1

0

显而易见的答案是对评论的回顾:

protected final char[] TOTAL_GBP = null;

不要设置null为 BigDecimal(TOTAL_GBP) @payment.setSubtotal(new BigDecimal(TOTAL_GBP)) 的构造函数参数;

示例: payment.setSubtotal(new BigDecimal("8.25")); 取自这里http://androiddevelopement.blogspot.com/2011/04/adding-paypal-payment-in-android.html关于 paypal 和 android 集成。

最后但同样重要的是:

您可能还想重新考虑您的AsyncTask流程和doInBackground()方法,并且您onPostExecute()通常不会onClick()AsyncTask. 他们宁愿必须在您的 Activity 中并在您的主线程中运行,然后在单击按钮时您可以调用AsyncTask.

有关 AsyncTasks 的更多信息:http: //developer.android.com/reference/android/os/AsyncTask.html AsyncTask Android 示例


希望它能澄清一点。

干杯

于 2013-03-22T22:06:12.170 回答