我见过一些相关的问题,但没有一个集中在我遇到的具体问题上:
我正在使用贝宝mpl库。
我构建paypalPayment对象,然后创建结帐活动。那很好。我的问题是,在resultdelegate上,我需要从我的活动中调用一个函数,该函数发生在付款后并进行一些更改(例如存储sharedpreferences等)。
所以像这样:

public class ResultDelegate implements PayPalResultDelegate, Serializable {
    public void onPaymentSucceeded(String payKey, String paymentStatus) {
        System.out.println("SUCCESS, You have successfully completed your transaction.");
        System.out.println("PayKey: "+payKey);
        System.out.println("PayStatus: "+paymentStatus);

        callMyCustomAfterPaymentFunction();
    }
    ...
}

现在的问题是,我试图为resultdelegate创建一个接受我的活动的构造函数。我现有的代码是:
//On the activity class
public class MainMenuActivity extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
        ...
        Button buy = (Button) findViewByID(R.id.buy_button);
        buy.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                new PurchaseTask(activity).execute();
            }
        }
    }
}

public class PurchaseTask extends AsyncTask <String, Void, String> {
    protected String doInBackground()
    {
        ...
        PayPal pp = PayPal.getInstance();
        CheckoutButton cb = pp.getCheckoutButton(...);
        cb.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                ResultDelegate delegate = new ResultDelegate(myActivity);
                Intent checkout = PayPal.getInstance().checkout(paument, activity, delegate);
                activity.StartActivity(checkoutIntent);
            }
        }
    }
}

//On the ResultDelegate class

public class ResultDelegate implements PayPalResultDelegate, Serializable {
    private Activity myActivity;

    public void onPaymentSucceeded(String payKey, String paymentStatus) {
        System.out.println("SUCCESS, You have successfully completed your transaction.");
        System.out.println("PayKey: "+payKey);
        System.out.println("PayStatus: "+paymentStatus);

        myActivity.performAfterPaymentOperations();
    }
    ...
}

所以目标是从resultdelegate调用activity函数。或者更简单一些,只是为了在resultdelegate onpaymentsucceeded()激发时能够存储一些sharedpreference更改。
但是我得到一个NotSerializableException,它提到MyActivity字段不可序列化。
所以,然后我在resultdelegate的activity字段中添加了瞬态标识符,但现在得到了一个nullpointerexception。

最佳答案

Paypal Mobile Chekout guide
贝宝网站上提供的实现与您的不同。他们正在做startActivityForResult()来启动PaypalActivity。在onActivityResult()方法中,他们检查statusCode以检查事务状态并相应地执行操作。
按照文件执行。
在你的代码中,我没有找到使用AsyncTask的意义。你的ResultDelegateSerializable而asActivity不是它抛出NotSerializableException的原因。
编辑:
既然你是为google android平台开发的,那为什么不在app中使用google checkout呢?
编辑:
此方法将在PaypalActivity完成时调用。这个活动将传递给这个方法。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) {
    case Activity.RESULT_OK:
        // The payment succeeded
        String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
        // Tell the user their payment succeeded
        break;
    case Activity.RESULT_CANCELED:
        // The payment was canceled
        // Tell the user their payment was canceled
        break;
    case PayPalActivity.RESULT_FAILURE:
        // The payment failed -- we get the error from the EXTRA_ERROR_ID
        // and EXTRA_ERROR_MESSAGE
        String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
        String errorMessage = data
                .getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
        // Tell the user their payment was failed.
    }
}

当做,
哈米德

08-25 22:44