我已经在我的Android应用程序中集成了PayPal。我有一个主要活动 - 关于活动,我在其中显示PayPal按钮。关于从主活动访问的活动。PayPal对象在线程中初始化,该线程从应用程序对象的 OnCreate 创建。
我现在面临两个问题:1.PayPal按钮仅在我第一次单击它时起作用。第二次不起作用。我必须返回主菜单,然后返回关于活动,然后它再次工作。这是:
将PayPal按钮添加到布局的代码:
mDonateButton = AppObj.Instance().GetPayPalObj().getCheckoutButton( mCaller,
PayPal.BUTTON_152x33,
CheckoutButton.TEXT_PAY );
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT );
mDonateButton.setLayoutParams(params);
mDonateButton.setGravity( Gravity.CENTER_HORIZONTAL );
mDonateButton.setOnClickListener( this );
LinearLayout container = (LinearLayout) findViewById( R.id.donateLayout );
container.addView(mDonateButton);
OnClick 相关代码:
PayPalPayment newPayment = new PayPalPayment();
newPayment.setSubtotal(new BigDecimal(Integer.parseInt(info)));
newPayment.setCurrencyType("USD");
newPayment.setRecipient("xxx@xxx.com");
newPayment.setPaymentType(PayPal.PAYMENT_TYPE_NONE);
newPayment.setMerchantName("xxx");
Intent paypalIntent = PayPal.getInstance().checkout(newPayment, mCaller);
(mCaller).startActivityForResult(paypalIntent, 1);
- 我有第二个问题....由于我在 BG 中初始化了 PayPal obj,如果我在PayPal完成初始化之前访问 About 活动(其中创建了 PayPal 按钮),我会崩溃......有什么想法吗?
谢谢约阿夫
如果您仍在寻找答案,我有一个
如果你看一下getCheckoutButton
方法,它会Context
作为参数,所以当Activity
是例如说暂停时,当你开始另一个Activity
时,CheckoutButton
的实例以某种方式丢失。
我修复的是通过使用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
Activity
初始化PayPal
库和CheckoutButton
,这是有效的。