2013-03-24 59 views
0

我在處理我的android應用程序中的PayPal沙盒支付時遇到困難。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代碼段:

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

回答

2
protected final char[] TOTAL_GBP=null; 

在這裏,你已經聲明TOTAL_GBPfinal char[]。你永遠不會(因爲它是final,你不能)重新定義這個變量,所以它永遠是null。因此,當你在這裏:

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

我不知道在哪裏fillCartList()定義,但)它不是new BigDecimal()之前調用,和b)它使用的TOTAL_GBP不同的定義。

您需要將BigDecimal所需的正確值轉換爲PaypalTaskTOTAL_GBP

+0

非常感謝您的回答。我在這裏有點困惑。這是否意味着我不需要爲'TOTAL_GBP'分配NULL值如果是這種情況,那麼我需要分配什麼值?有沒有可能用一個例子來解釋?再次感謝。 – 2013-03-25 00:03:29

+0

@ OliverSmith-Jones如果沒有在這裏看到您的確切設置,很難知道。 'fillCartList'是什麼類,並且是類中的'PaypalTask​​'? – Eric 2013-03-25 00:06:08

+0

'fillCartList'是一個單獨的方法來定義結帳清單和總量,總價格等。是的,'PaypalTask​​'本身定義爲一個類。 – 2013-03-25 00:11:10