Java PayPal支付活动中出现空异常
我在android应用程序中处理PayPal沙盒支付时遇到困难 一旦我点击PayPal按钮,我的应用程序就会崩溃,并给我一个空指针异常 以下是我的PayPalTask和PayPal按钮Java代码片段:Java PayPal支付活动中出现空异常,java,android,paypal,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) {
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();
}
}
});
}
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;
}
}
非常感谢您的建议和想法。非常感谢
Cartholder代码段:
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();
}
}
});
}
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;
}
}
私有静态类CartHolder{
text查看电视价格、电视标题、电视数量、电视总数;
公共图像视图图像;
}
私有类CartAdapter扩展了BaseAdapter{
私人充气机;
私有数组列表;
公共CartAdapter(ArrayList ll){
充气器=(LayoutFlater)活动选项卡。this.getSystemService(Context.LAYOUT\u充气器\u服务);
list=ll;
}
//此适配器链接数据集中的项目数。
public int getCount(){
返回list.size();
}
//获取与数据集中指定位置关联的数据项。
公共对象getItem(int位置){
返回列表。获取(位置);
}
//获取与列表中指定位置关联的行id。
公共长getItemId(int位置){
返回位置;
}
//获取在数据集中指定位置显示数据的视图。
公共视图getView(int位置、视图转换视图、视图组父视图){
车夫;
if(convertView==null){
convertView=充气机。充气(R.layout.item_cart,空);
支架=新支架();
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=(CartHolder)convertView.getTag();
}
位图bmp=BitmapFactory.decodeFile(cImage.get(position));
holder.tvQty.setText(String.valueOf(cQty.get(position));
int price=Integer.valueOf(cPrice.get(position).replace(“;”,”);
内部数量=cQty.get(位置);
整数合计=价格*数量;
holder.imgImage.setImageBitmap(bmp);
holder.tvTitle.setText(cTitle.get(position));
holder.tvPrice.setText(cPrice.get(position));
持有人.tvTotals.setText(总计+英镑);
返回视图;
}
}
在这里,您已将TOTAL_GBP
声明为final char[]
。您从未(而且,因为它是最终的
,所以您不能)重新定义此变量,因此它将始终为null
。因此,当你来到这里:
payment.setSubtotal(new BigDecimal(TOTAL_GBP)); // TOTAL_GBP has to be null
我不确定在哪里定义了fillCartList()
,但a)在new BigDecimal()
之前没有调用它,b)它使用了不同的TOTAL\u GBP
定义
您需要为
BigDecimal
到PaypalTask
的TOTAL_GBP
中获取所需的正确值。非常感谢您的回答。我在这里有点困惑。这是否意味着我不需要为TOTAL_GBP
分配空值?如果是这种情况,那么我需要分配什么值?可以用一个例子来解释吗?再次感谢。@OliverSmith Jones如果没有看到您在这里的确切设置,很难知道。什么类是类中的fillCartList
,什么类是类中的PaypalTask
?fillCartList
是一种单独的方法,用于定义结帐清单和总数量、总价等。是,PaypalTask
本身定义为类中的一个类。@OliverSmith Jones因此有一些类a
,PaypalTask
是a
的一个内部类,fillCartList
是a
的一个方法。@OliverSmith Jones我需要看到的是您已经展示的所有代码的包含类。基本上,您希望从PaypalTask
中除去protectedfinal char[]TOTAL_GBP
,然后使用外部类中的TOTAL_GBP
。(由fillCartList()
使用的一个)