PayPal付款活动中的空例外

时间:2013-03-24 23:48:51

标签: java android paypal

我在Android应用程序中处理PayPal沙盒付款时遇到了困难。

一旦我点击通过PayPal按钮付款,我的应用程序只会崩溃,并给我一个空指针异常。

这是我的PayPalTask​​和PayPal按钮Java代码片段:

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();
            }
        }   
    });
}

}

如果我更改以下行:

payment.setSubtotal(new BigDecimal(TOTAL_GBP));

到任何双浮点值,例如:

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

然后它的效果非常好,但从来没有从购物车中获取小计值,而只取得指定的总金额,即在这种情况下为£30.00

为了更加清晰,我的购物车列表Java代码片段如下:

private void fillCartList(boolean boo){
        // to delete the contents of the cartlists
        this.deleteCartLists();
        TOTAL_QTY = 0; TOTAL_GBP = 0;
        int price, qty;
        // if is not 0, first will parse all the rows to arraylists from sqlite stored on the phone
        Cursor cursor = db.getJoinedTables();
        if(cursor != null && cursor.getCount()!=0){
            cursor.moveToFirst();
            while(!cursor.isAfterLast()){
                if(boo == false){
                    price = Integer.valueOf(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BPRICE)).replace("£", ""));
                    qty = cursor.getInt(cursor.getColumnIndex(justdealsDatabase.KEY_CQTY));
                    cid.add(cursor.getInt(cursor.getColumnIndex(justdealsDatabase.KEY_BID)));
                    cTitle.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BTITLE)));
                    cCode.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BCODE)));
                    cPrice.add(price + "£");
                    cImage.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BIMAGE)));
                    cQty.add(qty);
                    TOTAL_QTY += qty;
                    TOTAL_GBP += qty * price;
                }else{
                    // to insert a new row to sales table on mysql db
                    postParameters.removeAll(postParameters);
                    price = Integer.valueOf(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BPRICE)).replace("£", ""));
                    qty = cursor.getInt(cursor.getColumnIndex(justdealsDatabase.KEY_CQTY));
                    //information will be parsed via PHP if the internet is working
                    postParameters.add(new BasicNameValuePair("bid", String.valueOf(cursor.getInt(cursor.getColumnIndex(justdealsDatabase.KEY_BID))))); 
                    Log.i(DEBUG, utils.prefs.getString("username", ""));
                    postParameters.add(new BasicNameValuePair("user", utils.prefs.getString("username", "")));  
                    postParameters.add(new BasicNameValuePair("sQty", String.valueOf(qty)));  
                    postParameters.add(new BasicNameValuePair("sDate", String.valueOf(getCurrentDate())));  
                    postParameters.add(new BasicNameValuePair("sTotal", String.valueOf(qty * price)));

                       String response = null;

                        try{
                            response = CustomHttpClient.executeHttpPost(Utils.LINK_SALES, postParameters).toString(); 
                            response = response.replaceAll("\\s+", "");
                            Log.i(DEBUG, response);
                        } catch (Exception e) {  
                            Log.e(DEBUG, "Error at fillCartList(): " + e.toString());  
                        }
                }
                cursor.moveToNext();
            }
        }
        tvValueTotalQty.setText("" + TOTAL_QTY);
        tvValueTotalGBP.setText(TOTAL_GBP + "£");
    }

您的建议和想法将受到高度赞赏。非常感谢。

Cartholder Code Snippet:

private static class CartHolder{
        TextView tvPrice, tvTitle, tvQty, tvTotals;
        public ImageView imgImage;
    }

    private class CartAdapter extends BaseAdapter{
        private LayoutInflater inflater;
        private ArrayList<String> list;

        public CartAdapter(ArrayList<String> ll){
            inflater = (LayoutInflater)ActivityTab.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            list = ll;
        }

        //number of items in the data set are linked by this Adapter.
        public int getCount() {
            return list.size();
        }

        // to get the data item associated with the specified position in the data set.
        public Object getItem(int position) {
            return list.get(position);
        }

        // to get the row id associated with the specified position in the list.
        public long getItemId(int position) {
            return position;
        }

        // to get a View that displays the data at the specified position in the data set. 
        public View getView(int position, View convertView, ViewGroup parent) {
            CartHolder holder; 
            if(convertView == null){
                convertView = inflater.inflate(R.layout.item_cart, null);
                holder = new CartHolder();
                holder.imgImage = (ImageView)convertView.findViewById(R.id.imgImage);
                holder.imgImage.getLayoutParams().height = 80;
                holder.imgImage.getLayoutParams().width = 80;
                holder.tvQty = (TextView)convertView.findViewById(R.id.tvNr);
                holder.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
                holder.tvPrice = (TextView)convertView.findViewById(R.id.tvItemPrice);
                holder.tvTotals = (TextView)convertView.findViewById(R.id.tvTotals);
                convertView.setTag(holder);
            }else{
                holder = (CartHolder)convertView.getTag();
            }
            Bitmap bmp = BitmapFactory.decodeFile(cImage.get(position));
            holder.tvQty.setText(String.valueOf(cQty.get(position)));
            int price = Integer.valueOf(cPrice.get(position).replace("£", ""));
            int qty = cQty.get(position);
            int total = price * qty;
            holder.imgImage.setImageBitmap(bmp);
            holder.tvTitle.setText(cTitle.get(position));
            holder.tvPrice.setText(cPrice.get(position));
            holder.tvTotals.setText(total+"£");

            return convertView;
        }
    }

1 个答案:

答案 0 :(得分:2)

protected final char[] TOTAL_GBP=null;

在此,您已将TOTAL_GBP声明为final char[]。你永远不会(并且,因为它是final,你不能)重新定义这个变量,所以它总是null。因此,当你到达这里时:

payment.setSubtotal(new BigDecimal(TOTAL_GBP)); // TOTAL_GBP has to be null

我不确定定义fillCartList()的位置,但a)在new BigDecimal()之前未调用它,b)它使用TOTAL_GBP的不同定义。

您需要为BigDecimal PaypalTask TOTAL_GBP获取{{1}}所需的正确值。