在Monogame中加载内容时出现ArgumentNullException

ArgumentNullException when loading content in Monogame
2020-10-22
  •  译文(汉语)
  •  原文(英语)

我无法加载纹理,我不知道为什么,因为我的代码非常简单.

namespace Rogue_Like_LOL
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        ScreenManager screenManager;

        public Game1()
            :base()
        {

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;
            Content.RootDirectory = "Content";

            Art.Load(Content);
        }
    }
}

和Art.cs:

    namespace Rogue_Like_LOL
    {
        static class Art
        {
            public static Texture2D Player { get; private set; }
            public static Texture2D PlayGame { get; private set; }
            public static Texture2D Exit { get; private set; }

            public static SpriteFont GameFont { get; private set; }

            public static void Load(ContentManager content)
            {
                // Player etc
                PlayGame = content.Load<Texture2D>("playgame");
                Exit = content.Load<Texture2D>("exit");

            }

    }

以下是异常详细信息:

未处理System.ArgumentNullException HResult = -2147467261
消息=空值.Nom duparamètre:图形设备不能为空Source = MonoGame.Framework ParamName =图形设备不能为空StackTrace:àMicrosoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice,Int32 width,Int32 height,Boolean mipmap,SurfaceFormat格式,SurfaceType类型,布尔共享,Int32 arraySize)àMicrosoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice,Int32 width,Int32 height)àMicrosoft.Xna.Framework.Graphics.Texture2D.PlatformFromStream(GraphicsDevice graphicsDevice, Stream流)àMicrosoft.Xna.Framework.Graphics.Texture2D.FromStream(GraphicsDevice graphicsDevice,Stream流)àMicrosoft.Xna.Framework.Content.ContentManager.ReadRawAsset [T](String assetName,

感谢您的关注.

速聊1:
您的纹理在"内容"中正确吗?不在任何子文件夹中?
速聊2:
是的,当然,我还检查了.png是否在"始终复制"上.
速聊3:
内容应通过覆盖其中的任何一种LoadContent(更合适)或Initialize虚拟的Game1方法加载.请注意,在覆盖Initialize时,请确保调用,base.Initialize()因为这实际上是在初始化基本游戏系统.
解决过程1

我建议您在准备使用内容管理器之后加载内容.我不确定这是否可以解决您的问题,但对我来说似乎是一个可能的问题.

为此:

protected override void LoadContent()
{
    Art.Load(Content);
}

I can't load textures I don't know why since my code is pretty straightforward.

namespace Rogue_Like_LOL
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        ScreenManager screenManager;

        public Game1()
            :base()
        {

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;
            Content.RootDirectory = "Content";

            Art.Load(Content);
        }
    }
}

And the Art.cs :

    namespace Rogue_Like_LOL
    {
        static class Art
        {
            public static Texture2D Player { get; private set; }
            public static Texture2D PlayGame { get; private set; }
            public static Texture2D Exit { get; private set; }

            public static SpriteFont GameFont { get; private set; }

            public static void Load(ContentManager content)
            {
                // Player etc
                PlayGame = content.Load<Texture2D>("playgame");
                Exit = content.Load<Texture2D>("exit");

            }

    }

Here are the Exception details :

System.ArgumentNullException was unhandled HResult=-2147467261
Message=La valeur ne peut pas être null. Nom du paramètre : Graphics Device Cannot Be Null Source=MonoGame.Framework ParamName=Graphics Device Cannot Be Null StackTrace: à Microsoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice, Int32 width, Int32 height, Boolean mipmap, SurfaceFormat format, SurfaceType type, Boolean shared, Int32 arraySize) à Microsoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice, Int32 width, Int32 height) à Microsoft.Xna.Framework.Graphics.Texture2D.PlatformFromStream(GraphicsDevice graphicsDevice, Stream stream) à Microsoft.Xna.Framework.Graphics.Texture2D.FromStream(GraphicsDevice graphicsDevice, Stream stream) à Microsoft.Xna.Framework.Content.ContentManager.ReadRawAsset[T](String assetName, String originalAssetName) à Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) à Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) à Rogue_Like_LOL.Art.Load(ContentManager content) dans c:\Users\Monique Dumont\Programmation\Rogue Like LOL\Rogue Like LOL\Art Manager\Art.cs:ligne 22 à Rogue_Like_LOL.Game1..ctor() dans c:\Users\Monique Dumont\Programmation\Rogue Like LOL\Rogue Like LOL\Game1.cs:ligne 34 à Rogue_Like_LOL.Program.Main() dans c:\Users\Monique Dumont\Programmation\Rogue Like LOL\Rogue Like LOL\Program.cs:ligne 17 à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ThreadHelper.ThreadStart_Context(Object state) à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart() InnerException:

Thanks for your attention.

Talk1:
your textures are in "Content" right? not in any subfolder?
Talk2:
Yes of course, I also checked if the .png were on "Copy always".
Talk3:
Content should be loaded either in LoadContent (more appropriate) or Initialize virtual methods of Game1 by overriding either of these. Note that when overriding Initialize, make sure to call base.Initialize() because that is what is actually initializing base game systems.
Solutions1

I suggest you to load your content after the content manager is ready to be used. I am not sure if this would solve your problem, but it seems like a possible issue to me.

To do so:

protected override void LoadContent()
{
    Art.Load(Content);
}
转载于:https://stackoverflow.com/questions/30576812/argumentnullexception-when-loading-content-in-monogame

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入