【问题标题】:App works fine in simulator but throws NSUnknownKeyException exception on device应用程序在模拟器中运行良好,但在设备上抛出 NSUnknownKeyException 异常
【发布时间】:2014-03-28 09:07:30
【问题描述】:

我正在尝试在 Xamarin Studio 中创建一个简单的 Hello World 测试应用程序。我创建了一个没有 Story Board 的空白 Single View iPhone 应用程序。

我在视图上拖放一个 ImageView 并为其连接一个插座。就这么简单。

当我在模拟器中运行时,它工作正常,但在设备上调试时会抛出 NSUnknownKeyException。

我检查了通常的嫌疑人:

  1. 用感叹号检查未定义的插座
  2. 检查了 Designer.cs 文件
  3. 检查 .h 和 .m 文件
  4. 全部清理并重建
  5. 从部署选项中清除主界面设置
  6. 设置自定义类定义
  7. 使用 -f 选项编译
  8. 更新到最新的 Xcode、Monotouch 和 Xamarin Studio(Xamarin Studio 4.2.3、Xcode 5.1、7.2.0.2(入门版)
  9. 将 iPhone 5s 更新到 IOS 7.1

我仍然遇到同样的异常。以下是代码和错误信息。我是否在这里遗漏了一个非常明显的事情,或者 Xcode 和 Xamarin Studio 之间的同步失败了?

感谢您的帮助。

错误日志

MonoTouch.Foundation.MonoTouchException:抛出 Objective-C 异常。 名称:NSUnknownKeyException 原因:[setValue:forUndefinedKey:]:这个类不是键值 键 imgTest 的编码兼容。在 at (包装 托管到本机)MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr) 在 MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] 在 /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:130 在 TestProject.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication 应用程序,MonoTouch.Foundation.NSDictionary 选项)[0x00038] 在 /Users/gorken/Projects/TestProject/TestProject/AppDelegate.cs:31 在 在(包装器托管到本机) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 在 MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 在 TestProject.Application.Main (System.String[] args) [0x00008] 中 /Users/gorken/Projects/TestProject/TestProject/Main.cs:16

TestProjectViewController.cs

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace TestProject
{
    public partial class TestProjectViewController : UIViewController
    {
        public TestProjectViewController () : base ("TestProjectViewController", null)
        {
        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            // Perform any additional setup after loading the view, typically from a nib.
        }
    }
}

TestProjectViewController.designer.cs

using MonoTouch.Foundation;
using System.CodeDom.Compiler;

namespace TestProject
{
    [Register ("TestProjectViewController")]
    partial class TestProjectViewController
    {
        [Outlet]
        MonoTouch.UIKit.UIImageView imgTest { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (imgTest != null) {
                imgTest.Dispose ();
                imgTest = null;
            }
        }
    }
}

AppDelegate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace TestProject
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the
    // User Interface of the application, as well as listening (and optionally responding) to
    // application events from iOS.
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        // class-level declarations
        UIWindow window;
        TestProjectViewController viewController;
        //
        // This method is invoked when the application has loaded and is ready to run. In this
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);

            viewController = new TestProjectViewController ();
            window.RootViewController = viewController;
            window.MakeKeyAndVisible ();

            return true;
        }
    }
}

TestProjectViewController.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface TestProjectViewController : UIViewController {
}
@property (retain, nonatomic) IBOutlet UIImageView *imgTest;

@end

TestProjectViewController.m

#import "TestProjectViewController.h"

@implementation TestProjectViewController

- (void)dealloc {
    [_imgTest release];
    [super dealloc];
}
@end

xib 文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TestProjectViewController">
            <connections>
                <outlet property="imgTest" destination="PLQ-yP-dMH" id="f4J-3q-Qoq"/>
                <outlet property="view" destination="6" id="7"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="6">
            <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="PLQ-yP-dMH">
                    <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                </imageView>
            </subviews>
            <color key="backgroundColor" white="0.75" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
            <simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
        </view>
    </objects>
</document>

【问题讨论】:

    标签: c# ios xcode xamarin


    【解决方案1】:

    在快速谷歌之后,我发现清理和重建您的解决方案有助于清除此错误 (https://bugzilla.xamarin.com/show_bug.cgi?id=15002)。 要清理和重建您的解决方案,请在 Xamarin 工作室中,转到屏幕顶部的“构建”,然后单击“全部清理”。之后,点击“全部重建”。

    如果这对您不起作用,请查看“主界面”设置中的 info.plist。那里的错误可能会引发此异常。

    此异常的另一个原因可能是当接口生成器中不再存在的控件存在插座时。再次检查设计器文件(如果一切正常,可以使用 Xcode 进行额外检查)。

    如果这对您有帮助,请告诉我。祝你好运!

    爱与问候, 比约恩

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多