【问题标题】:Xamarin SKPaymentQueue AddPayment throwing exceptionXamarin SKPaymentQueue AddPayment 抛出异常
【发布时间】:2013-12-18 03:35:09
【问题描述】:

当我调用此方法时在我的 Xamarin 应用程序中

private void MakePayment (SKProduct product)
{
    SKPayment payment = SKPayment.PaymentWithProduct (product);
    SKPaymentQueue.DefaultQueue.AddPayment (payment);
}

我收到此错误:

无法编组 Objective-C 对象 0x14607110(类型: SKPaymentTransaction)。找不到现有的托管实例 这个对象,也不能创建一个新的托管实例 (因为类型 'MonoTouch.StoreKit.SKPaymentTransaction[]' 不 有一个接受一个 IntPtr 参数的构造函数)。

我不确定我的配置是否有问题,或者我的代码或 Xamarin 是否存在问题。

这是观察者的代码

internal class CustomPaymentObserver : SKPaymentTransactionObserver
{
    private InAppPurchase inAppPurchase;

    public CustomPaymentObserver (InAppPurchase inAppPurchase)
    {
        this.inAppPurchase = inAppPurchase;
    }

    public override void UpdatedTransactions (SKPaymentQueue queue, SKPaymentTransaction[] transactions)
    {
        Console.WriteLine ("UpdatedTransactions");
        foreach (SKPaymentTransaction transaction in transactions) {
            switch (transaction.TransactionState) {
            case SKPaymentTransactionState.Purchased:
                inAppPurchase.CompleteTransaction (transaction);
                break;
            case SKPaymentTransactionState.Failed:
                inAppPurchase.FailedTransaction (transaction);
                break;
            default:
                break;
            }
        }
    }

    public override void PaymentQueueRestoreCompletedTransactionsFinished (SKPaymentQueue queue)
    {
    }

    public override void RestoreCompletedTransactionsFailedWithError (SKPaymentQueue queue, NSError error)
    {
    }
}

这是完整的堆栈跟踪:

System.Exception: Failed to marshal the Objective-C object 0x17ecb680 (type: SKPaymentTransaction). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.StoreKit.SKPaymentTransaction[]' does not have a constructor that takes one IntPtr argument).
at MonoTouch.ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:352
at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in :0
at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, System.Boolean& created) [0x00073] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:514
at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) [0x0000c] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:686
at at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr,intptr,int&)
at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr)
at MonoTouch.StoreKit.SKPaymentQueue.AddPayment (MonoTouch.StoreKit.SKPayment payment) [0x00014] in /Developer/MonoTouch/Source/monotouch/src/StoreKit/.pp-SKPaymentQueue.g.cs:109
at IOS.Util.IAP.InAppPurchase.ReceivedResponse (MonoTouch.StoreKit.SKProductsRequest request, MonoTouch.StoreKit.SKProductsResponse response) [0x0001d] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Util/IAP/InAppPurchase.cs:43
at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pp-UIApplication.cs:38
at IOS.Application.Main (System.String[] args) [0x00008] in /Users/aaron/Projects/budget-ease-xamarin/IOS/Main.cs:16

【问题讨论】:

  • SKPaymentTransactionObserverUpdatedTransactions方法中使用了一个SKPaymentTransaction[],你能把你的代码发给观察者吗?
  • 我添加了观察者代码和完整的堆栈跟踪。不过,它似乎并没有到达观察者那里。
  • 这似乎与项目中的链接器行为有关。我将其更改为 Don't Link,现在我没有收到此错误。我应该把它放在不链接上吗?
  • 我只会使用Link SDK Assemblies Only,你是在链接所有内容吗?
  • 我只使用了 Link SDK 程序集,似乎链接器正在删除错误消息中提到的构造函数。我不知道为什么,但卸载 Xamarin 和 MonoTouch 并重新安装似乎已经解决了它。

标签: xamarin.ios in-app-purchase xamarin


【解决方案1】:

这里发生的情况是,您的 CustomPaymentObserver 的 C# 实例被垃圾收集,而它的本机(“Objective-C”)对应物仍然存在。当最终发送通知时,本机对象尝试调用现在已死的 C# 对象并使您的应用程序崩溃。

要避免这种情况,请保留对您的 CustomPaymentObserver 的引用,例如在您的 AppDelegate 中以使其保持活力。

我不确定这是否记录在 Xamarin.iOS 的某处(无法快速查看),但我相信 Xamarin.Droid 非常相似(例如http://developer.xamarin.com/guides/android/advanced_topics/garbage_collection/

【讨论】:

    【解决方案2】:

    我不确定是什么原因造成的。我认为这可能与链接器有关。但我从我的 Mac 上完全卸载了 Xamarin(包括所有 MonoTouch 的东西)并重新安装了所有东西,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2016-04-27
      • 2015-09-06
      • 2019-04-02
      • 2011-05-30
      相关资源
      最近更新 更多