0

我在我的 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 + "£");
    }

您的建议和想法将不胜感激。非常感谢。

车架代码片段:

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;
        }
    }
4

1 回答 1

2
protected final char[] TOTAL_GBP=null;

Here, you've declared TOTAL_GBP as a final char[]. You never (and, because it's final, you cannot) redefine this variable, so it will always be null. Thus, when you get to here:

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

I'm not sure where fillCartList() is defined, but a) it's not called before new BigDecimal(), and b) it uses a different definition of TOTAL_GBP.

You need to get the correct value that you want for the BigDecimal into PaypalTask's TOTAL_GBP.

于 2013-03-24T23:55:32.377 回答