简体   繁体   English

在Monogame中加载内容时出现ArgumentNullException

[英]ArgumentNullException when loading content in Monogame

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 : 和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 未处理System.ArgumentNullException 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 conte 参数值:图形设备不能为空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 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 conte nt) 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(E nt)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程序集,String [] args)àSystem.AppDomain.ExecuteAssembly(String assemblyFile,证据assemblySecurity,String [] args)àMicrosoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()àSystem.Threading.ThreadHelper.ThreadStart_Context (对象状态)àSystem.Threading.ExecutionContext.RunInternal(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔值saveSyncCtx)àSystem.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔值saveSyncCtx)àSystem.Threading .ExecutionContext.Run(E xecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart() InnerException: xecutionContext executeContext,ContextCallback回调,对象状态)àSystem.Threading.ThreadHelper.ThreadStart()InnerException:

Thanks for your attention. 感谢您的关注。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM