Porting a game from XNA to Android, nothing works, errors everywhere.


I have not been this frustrated in my entire life.
I was hoping to finally port a game to Android, and then I get hit in the face by Android and its errors.
I find no help online, and I’m a good googler.
It feels like I am the only one in the world that does not get Android to work, so I will have to try to get some help here, I guess.

Please help me, I will try to explain the problem as good as I can.

So, I have a perfectly working game on XNA, which I then ported to Windows Phone 7.1, which works perfectly fine, which I then ported to Android through Xamarin and MonoGame.
But then shit hit the fan, so to speak.

The first thing that happened was that the emulator just doesn’t work, it gives this error:

Failed to create desired format, falling back to defaults

Then crashes. So on the forums it says that that happens on every emulator that is not above version Android 4.0.3, but it doesnt work even on Android version 4.0.3, or any other version. So I have to borrow someone else’s Samsung S3. Which gives other errors.

The second thing that happened was that when I try to define a variable with the value from the display’s width and height, graphics.GraphicsDevice is null.

ScreenDimension = new Vector2(this.graphics.GraphicsDevice.DisplayMode.Height, this.graphics.GraphicsDevice.DisplayMode.Width);

Why? I have no idea. So I googled that and found this: http://stackoverflow.com/questions/4028756/xna-graphicsdevice-initialization-confusion
There it says that I need to add this above the previously mentioned code:

this.graphicsDeviceManager = this.Services.GetService(typeof(IGraphicsDeviceManager)) as IGraphicsDeviceManager;
if (this.graphicsDeviceManager != null)

All this is done in the Constructor in Game1.cs
But when I did that, it gets even weirder.
Nothing will load, it never even gets to the LoadContent function (??), everything is null, everything is “Unknown Identifier”, and so on. I don’t understand. I have made the content to .xnb, but it doesn’t work. SpriteFont is an unknown identifier, Vector2 is an unknown identifier… It just goes on and on…
I finally managed to get the game to get to the Update function, but it just stops first to tell me that the thing I try to update is null, because the game never even gets to LoadContent, where that variable get defined.
I don’t understand anything, it seems to work for everyone else. :frowning:

Please download my project and test by yourself:

Any help is extremely appreciated.


First, don’t bother trying to run MonoGame application in the official emulator, it won’t work since it don’t support OpenGL.

Then, I see a few blocking problems:

  • remove Program.cs, it is only relevant to Windows platforms. Android app entry point are Activities (see Activity1.cs)
  • components (FrameRateCounter) should be initialized the Initilize method
  • don’t try to alter the target framerate, Android apps are capped to 60fps by the system itself (remove TargetElapsedTime modification) and it may result in errors
  • move you graphicsDevice initialization to the Initialize methods, the constructor does not ensure that it has been initialized yet
  • for WP project as well as Android ones, you don’t have to set the resolution to the screen resolution, the framework will by it self set it (this with the 2 previous points sum in removing any code related to this.graphics, since what you try to do is not supported on Android or simply the default values)
  • there’s a few inheritence problems: don’t comment base.Initialize() or base.LoadContent() and add “: base()” on your constructor or else you mess the Game class behavior (and LoadContent is never correctly called)

Fixing those points makes the game run on Android.

The blocking issues are mostly C# mistakes (mainly incomplete inheritence, like omitting base class call and overriding methods not correctly, or misunderstand use of “virtual” keyword) and not related to XNA/MonoGame.
My best advice would be to read some more C# documentation related to class and inheritence and to rely less on copy/pasting google stuff.

Thank you so much for helping me!

I have tried to solve the problems but it still doesn’t work.
The constructor now looks like this:

 public Game1() : base()
            instance = this;
            Content.RootDirectory = "Content";

            //this.IsFixedTimeStep = true; // makes the timestep fixed
            //this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 120.0f); // the target FPS
            showFPS = true;

Because I moved the graphicsDevice references to the Initialize function instead. I also moved the FPS references to the Initialize function. I added : base() to the constructor.

The initialize function:

protected override void Initialize()
            graphics = new GraphicsDeviceManager(this);

            graphics.SupportedOrientations =
                   DisplayOrientation.LandscapeLeft |


            //graphics.SynchronizeWithVerticalRetrace = false; // vsync to prevent tearing and lock fps
            this.graphics = this.Services.GetService(typeof(IGraphicsDeviceManager)) as IGraphicsDeviceManager;
            if (this.graphics != null)

            ScreenDimension = new Vector2(this.graphics.GraphicsDevice.DisplayMode.Height, this.graphics.GraphicsDevice.DisplayMode.Width);

            FPS = new FramerateCounter(this);

I commented out the references to graphics.ToggleFullScreen and graphics.VerticalRetrace. If that were the problem.
I added the graphics inititalizer to the initialize function, but it gives an error:

Cannot implicitly convert type 'Microsoft.Xna.Framework.IGraphicsDeviceManager' to 'Microsoft.Xna.Framework.GraphicsDeviceManager'. An explicit conversion exists (are you missing a cast?)

I think it’s because it says in the initliazer code IGraphicsDeviceManager, and not GraphicsDeviceManager, but that’s what it said on the forums that I should use.

Also I removed the comment-marks on the base.Initialize line.
Also, where should i call base.LoadContent? Why does not LoadContent get loaded?

Sorry for bad english, tired and native Swedish speaker.

You have introduced a recursive infinite call with the line this.Initialize(); in the Initialize method…

Moreover, this belongs to the constructor (sorry for the misleading instructions):

graphics = new GraphicsDeviceManager(this);

graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;

LoadContent was never called because you omitted to call base.Initialize and/or the base class constructor.

And this, is irrelevant, just remove it:

            this.graphics = this.Services.GetService(typeof(IGraphicsDeviceManager)) as IGraphicsDeviceManager;
        if (this.graphics != null)

Thanks once again for answering and helping.

I moved back that code, and removed the code that you said was irrelevant.

I removed the recursive loop and removed base.Initialize();
I instead added base.LoadContent(); so that it will load the content, but LoadContent still won’t load.

I’m sorry if I didn’t understand, I have just learned that in XNA it will load automatically.

The loop was induced by THIS.Initialize, not BASE.Initialize (which should be left untouched).

Again, you should learn C# basics before going farther into XNA. :slight_smile:

Thank you again!
It works now, it doesn’t crash! I can’t believe it!
I learn C# and XNA at my school and I learn what the teacher tells me to do. But of course, I can learn on my own hand as well.

I have one problem though, everything seems to render very weird. Of course, the game is made for 4:3 display, but it renders at the wrong positions.
For example, the player spawns under the screen, and the games seems to think that the screen dimension’s height is taller. (the player should be stopped at the bottom of the screen.)
And the frame I have rendered spawns in the top right corner…

The player spawns with this:

    Position.X = Game1.ScreenDimension.X / 3;
    Position.Y = Game1.ScreenDimension.Y / 2;

But the player spawns perfectly in the X axis, but under the screen (Y).
Why does some things render perfectly, and some things very weird. I had problems with this on PC as well, but I thought that they got fixed. :frowning:

This makes my head hurt…

I have changed to that ScreenDimension.X is equals to the width, and .Y to the height. So now the graphics renders fine, for the most part…

For instance, the player. The player spawns under the screen, and too near the end of the screen.
It spawns with this:

Position.X = Game1.ScreenDimension.X / 3;
Position.Y = Game1.ScreenDimension.Y / 2;

This gives it the following positions when debugged:
Which should be perfect, but in reality, when playing, it is near the end of the screen of the X axis, and under the screen on the Y axis.
Can you take a look at my code to check why this happens?