1

我正在實現支付網關。我有一個數組適配器和在這個陣列適配器我對活動結果的方法,我如何調用此方法從arrayadapter調用onactivityResult方法

這裏是我的代碼

//這是我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) 
+0

你的問題是什麼? – Jas

+0

我的問題是我不能夠調用onactivityresult方法就拋出異常 – user2894408

+0

這裏是例外 致命異常:主要 過程:com.dt.whosatthedoor java.lang.ClassCastException:android.app.Application不能轉換到Android。 support.v4.app.FragmentActivity com.dt.service.CustomBuySpaceAdapter.onBuyPressedcom.dt.service.CustomBuySpaceAdapter $ 1.onClick android.view.View.performClick android.view.View $ PerformClick.runandroid.os.Handler.handleCallbackandroid .os.Handler.dispatchMessage – user2894408

回答

2

嘿,這方法只與該活動相關聯,以這種方法必須是在活動,以便把你的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."); 
     } 
    } 
} 
0

在適配器類中不能使用onActivityResult(int requestCode, int resultCode, Intent data)。它只能用於Activity。在使用適配器類的Activity中編寫該方法。

+0

你的意思是我必須在活動中編寫onActivityResult方法,然後我怎麼從適配器調用它 – user2894408

+0

你在列表視圖項目中做什麼動作? – Jas

+0

我正在使用imageview上的點擊偵聽器並使用視圖持有者 – user2894408

相關問題