从arrayadapter调用onactivityresult方法

时间:2015-12-09 08:03:18

标签: android android-fragments android-intent android-activity

我正在实施支付网关。我有一个数组适配器,在这个数组适配器中我有活动结果方法我如何调用这个方法

这是我的代码

//这是我的arrayadapter中的代码

public void onBuyPressed(View pressed) {
    /*
     * PAYMENT_INTENT_SALE will cause the payment to complete immediately.
     * Change PAYMENT_INTENT_SALE to - PAYMENT_INTENT_AUTHORIZE to only
     * authorize payment and capture funds later. - PAYMENT_INTENT_ORDER to
     * create a payment for authorization and capture later via calls from
     * your server.
     * 
     * Also, to include additional payment details and an item list, see
     * getStuffToBuy() below.
     */
    PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);

    /*
     * See getStuffToBuy(..) for examples of some available payment options.
     */

    Intent intent = new Intent(context, PaymentActivity.class);

    // send the same configuration for restart resiliency
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

    ((Activity) context).startActivityForResult(intent, 1);

}

//这是我的OnactivityResultMethod

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    System.out.println("OnActivityCalled");

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data
                    .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    System.out
                            .println("Print 1 ............................"
                                    + confirm.toJSONObject().toString(4));
                    System.out
                            .println("print 2 ........................... "
                                    + confirm.getPayment().toJSONObject()
                                            .toString(4));

                    Toast.makeText(
                            BuySpaceActivity.this,
                            "PaymentConfirmation info received from PayPal",
                            Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    System.out
                            .println("an extremely unlikely failure occurred: "
                                    + e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            System.out.println("The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            System.out
                    .println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

这是错误日志

12-09 13:40:26.853: E/AndroidRuntime(25986): FATAL EXCEPTION: main
12-09 13:40:26.853: E/AndroidRuntime(25986): Process: com.dt.whosatthedoor, PID: 25986
12-09 13:40:26.853: E/AndroidRuntime(25986): java.lang.ClassCastException: android.app.Application cannot be cast to android.support.v4.app.FragmentActivity
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.dt.service.CustomBuySpaceAdapter.onBuyPressed(CustomBuySpaceAdapter.java:130)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.dt.service.CustomBuySpaceAdapter$1.onClick(CustomBuySpaceAdapter.java:99)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.view.View.performClick(View.java:4757)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.view.View$PerformClick.run(View.java:19757)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Handler.handleCallback(Handler.java:739)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.os.Looper.loop(Looper.java:135)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at android.app.ActivityThread.main(ActivityThread.java:5233)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at java.lang.reflect.Method.invoke(Native Method)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at java.lang.reflect.Method.invoke(Method.java:372)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
12-09 13:40:26.853: E/AndroidRuntime(25986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

2 个答案:

答案 0 :(得分:3)

嘿这个方法只与活动相关联,所以这个方法必须在活动中,所以拿你的onActivityResult方法把它放在活动上,你也检查你在arrayadapter中传递的上下文

你应该这样做

BuySpaceActivity act = (BuySpaceActivity) context;
    act.startCommentActivity(intent);

//在您的活动中制作此方法

public void startCommentActivity(Intent i) {
    startActivityForResult(i, 1);
}

//你的onACtivityResult方法

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    System.out.println("OnActivityCalled");

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data
                    .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    System.out
                            .println("Print 1 ............................"
                                    + confirm.toJSONObject().toString(4));
                    System.out
                            .println("print 2 ........................... "
                                    + confirm.getPayment().toJSONObject()
                                            .toString(4));

                    Toast.makeText(
                            BuySpaceActivity.this,
                            "PaymentConfirmation info received from PayPal",
                            Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    System.out
                            .println("an extremely unlikely failure occurred: "
                                    + e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            System.out.println("The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            System.out
                    .println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }
}

答案 1 :(得分:0)

您不能在适配器类中使用onActivityResult(int requestCode, int resultCode, Intent data)。它仅用于Activity。在使用适配器类的Activity中编写方法。