【问题标题】:AndroidGameWindow.SetDisplayOrientation NullReferenceExceptionAndroidGameWindow.SetDisplayOrientation NullReferenceException
【发布时间】:2014-09-03 22:55:35
【问题描述】:

我使用 MonoGame 和 Xamarin 为 Android 开发了一款游戏。我将 BugSense 加入其中并迅速开始获取以下异常堆栈跟踪:

System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.Xna.Framework.AndroidGameWindow.SetDisplayOrientation (Microsoft.Xna.Framework.DisplayOrientation) <0x001c4>
at Microsoft.Xna.Framework.AndroidGameWindow.SetOrientation (Microsoft.Xna.Framework.DisplayOrientation,bool) <0x00097>
at Microsoft.Xna.Framework.OrientationListener.OnOrientationChanged (int) <0x001c7>
at Android.Views.OrientationEventListener.n_OnOrientationChanged_I (intptr,intptr,int) <0x0003f>
at (wrapper dynamic-method) object.ed9d7c7c-f3e6-4d7a-9249-1a139a251aed (intptr,intptr,int) <0x00043>

这就是我的活动设置方式:

[Activity(Label = "My Cool Game"
        , MainLauncher = true
        , Icon = "@drawable/icon"
        , Theme = "@style/Theme.Splash"
        , AlwaysRetainTaskState = true
        , LaunchMode = Android.Content.PM.LaunchMode.SingleTask
        , ScreenOrientation = ScreenOrientation.Portrait
        , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden)]

在游戏构造函数中我有以下内容:

public Game1()
{
    _graphics = new GraphicsDeviceManager(this);
    _graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
    _graphics.IsFullScreen = true;
    _graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.PortraitDown;
    _graphics.ApplyChanges();
}

项目设置为针对 v2.3 进行编译。我的测试设备上没有与方向有关的任何问题,所以我不确定是什么导致了这个异常,因此我无法修复它。

【问题讨论】:

  • 我很确定 _graphics 不会在 Game1 的构造函数中初始化。您可能需要将代码移动到 Game1 的 Initialize 方法中。
  • @craftworkgames 我已经编辑了构造函数代码,我最初没有包含所有的图形线。但是,如果这无关紧要,您是说这可能是异常的原因吗?如果是这样,在我向应用推送更新之前如何确定?
  • 实际上,我的最后一条评论来自我(显然很糟糕)的记忆。我刚刚检查了我的代码,结果发现我错了。 _graphics 字段当然可以在构造函数中初始化。我看到您已经更新了问题中的代码,对我来说看起来不错。很抱歉之前的误导性评论。
  • 您是否获得了有关发生这些异常的设备/平台的其他信息?是指设备类型及其运行的 Android 操作系统的版本?
  • @jensendp 它发生在运行 4.1.2 及更高版本的设备上。我还刚刚注意到,BugSense 将这些异常报告为正在处理的异常。

标签: c# android xamarin.android monogame


【解决方案1】:

在处理其他项目之前,我遇到了这个空引用异常。您将不得不原谅我对您在游戏中的特定领域的无知,但让我向您解释这意味着什么,也许它会帮助您解决这个问题。此异常意味着内存中没有特定类型的对象可供系统使用。必须使用 new 关键字或其他方式创建此对象的实例,才能将其识别为内存中的有效对象。

通过阅读上述错误消息,似乎需要一个对象来初始化显示方向。在您的构造函数或活动设置中,我没有看到任何为显示方向创建任何对象的东西。因此该对象不在内存中,所以它为空。

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多