gpt4 book ai didi

java - PayPal 支付 Activity 中的空异常

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:40 26 4
gpt4 key购买 nike

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

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

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

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"));

然后它工作得很好,但从不从购物车中获取小计值(value),而只获取指定的总金额,即在本例中为 30.00 英镑

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

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

非常感谢您的建议和想法。非常感谢。

持卡人代码 fragment :

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

最佳答案

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

关于java - PayPal 支付 Activity 中的空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com