Android Paypal启动按钮OnClick在活动生命周期内仅执行一次

Android Paypal启动按钮OnClick在活动生命周期内仅执行一次,android,paypal,onclick,onclicklistener,Android,Paypal,Onclick,Onclicklistener,这是破坏我的大脑,当你进入活动,你可以点击贝宝按钮,你进入贝宝活动罚款。如果取消paypal活动,您将返回原始活动。从这里开始,如果你再次点击按钮,什么也不会发生。我试着用OnClick方法将一条基本消息打印到控制台,但它甚至没有显示出来,似乎没有注册点击。我假设问题与布局xml文件或清单文件无关。我想发布一张图片,但我不能,因为我是新的,你需要知道的是,这是一个带有贝宝按钮的结账屏幕 提前感谢,, 休 package com.cit.datastructuretesting; import

这是破坏我的大脑,当你进入活动,你可以点击贝宝按钮,你进入贝宝活动罚款。如果取消paypal活动,您将返回原始活动。从这里开始,如果你再次点击按钮,什么也不会发生。我试着用OnClick方法将一条基本消息打印到控制台,但它甚至没有显示出来,似乎没有注册点击。我假设问题与布局xml文件或清单文件无关。我想发布一张图片,但我不能,因为我是新的,你需要知道的是,这是一个带有贝宝按钮的结账屏幕

提前感谢,, 休

package com.cit.datastructuretesting;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalPayment;

public class StoreItemInterface extends Activity implements OnClickListener, OnKeyListener
{           
Double subtotal, shipping, total;
EditText etItemQuantity;
TextView tvItemSubtotal, tvItemTotal;
DecimalFormat decFormat = new DecimalFormat("#.##");
PayPal ppObj;
CheckoutButton launchPayPalButton;



@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);     

    setContentView(R.layout.storeiteminterface);

    ImageView ivItemImage = (ImageView) findViewById(R.id.ivItemImage2);
    TextView tvItemTitle = (TextView) findViewById(R.id.tvItemTitle2);
    TextView tvItemDescription = (TextView) findViewById(R.id.tvItemDescription2);
    etItemQuantity = (EditText) findViewById(R.id.etItemQuantity);
    tvItemSubtotal = (TextView) findViewById(R.id.tvItemSubtotal);
    TextView tvItemShipping = (TextView) findViewById(R.id.tvItemShipping);
    tvItemTotal = (TextView) findViewById(R.id.tvItemTotal);    

    subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
    shipping = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getShippingPrice();
    total = subtotal + shipping;

    ivItemImage.setImageBitmap(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getImageBitmap());            
    tvItemTitle.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getTitle());
    tvItemDescription.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getDescription());
    tvItemSubtotal.setText("€" + decFormat.format(subtotal));       
    tvItemShipping.setText("€" + decFormat.format(shipping));       
    tvItemTotal.setText("€" + decFormat.format(total)); 

    etItemQuantity.setOnKeyListener(this);

    //setup paypal stuff
    ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);

    launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.bottomMargin = 10;

    launchPayPalButton.setLayoutParams(params);        

    ((RelativeLayout)findViewById(R.id.paypalLayout)).addView(launchPayPalButton); 

    launchPayPalButton.setOnClickListener(this);
}

@Override
public void onClick(View arg0) 
{       
    if(arg0.getId() == launchPayPalButton.getId())
    {
        System.out.println("TEST");

        if(!tvItemTotal.getText().toString().equals("---"))
        {
            PayPalPayment newPayment = new PayPalPayment();
            newPayment.setSubtotal(BigDecimal.valueOf(total));
            newPayment.setCurrencyType("EUR");
            newPayment.setRecipient("Cape_1328492032_biz@mycit.ie");
            newPayment.setMerchantName("Cape Clear App");

            Intent paypalIntent = PayPal.getInstance().checkout(newPayment, StoreItemInterface.this);
            StoreItemInterface.this.startActivityForResult(paypalIntent, 1);
        }
        else
        {
            Toast.makeText(StoreItemInterface.this, "Invalid Quantity!", Toast.LENGTH_SHORT).show();
        }
    }       
}

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) 
{       
    if(etItemQuantity.getText().toString() != null && !etItemQuantity.getText().toString().trim().equals("") && !etItemQuantity.getText().toString().trim().equals("0"))
    {
        subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
        total = subtotal + shipping;

        tvItemSubtotal.setText("€" + subtotal);         
        tvItemTotal.setText("€" + decFormat.format(total));
    }
    else
    {
        tvItemSubtotal.setText("---");          
        tvItemTotal.setText("---");         
    }

    return false;
}   

}

在onClick()完成后,尝试启动PayPalButton.updateButton()


如果你还在寻找答案,我有一个

如果您查看
getcheckburatton
方法,它将
上下文
作为参数,因此当
活动
为时,例如当您启动另一个
活动时发生的暂停时,
checkburatton
的实例不知何故丢失

我通过在活动的onResume中使用
updateButton
方法修复了此问题

@Override
protected void onResume() {
    /**
     * The CheckoutButton has to be updated each time the Activity is
     * resumed, otherwise the onClickListener of CheckoutButton will not work
     **/
    if (mCheckOutBtn != null && (mCheckOutBtn instanceof CheckoutButton))
        mCheckOutBtn.updateButton();
    super.onResume();
}

考虑到您在
活动的
onCreate
中初始化了
PayPal
库和
checkburatton
,此操作有效:logcat中没有错误消息。。。