gpt4 book ai didi

android - 从 arrayadapter 调用 onactivityresult 方法

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:26 28 4
gpt4 key购买 nike

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

这是我的代码

//这是我的数组适配器中的代码

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)

最佳答案

嘿,这个方法只与 Activity 相关,所以这个方法必须在 Activity 中,所以把你的 onActivityResult 方法放在 Activity 上,你还要检查你在数组适配器中传递的上下文

你应该这样做

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

//在你的activity中创建这个方法

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.");
}
}
}

关于android - 从 arrayadapter 调用 onactivityresult 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com