I am getting quite a few bug reports from BugSense that go as follows:
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>
The app runs just fine on my 2 Android devices and I had never seen an issue with device orientation. I have my activity setup as follows:
I noticed that activity’s ScreenOrientation has many more portrait values other than ScreenOrientation.Portrait, like reversePortrait & sensorPortrait. Maybe I should add those since I set _graphics to both Portrait & PortraitDown.
Well it seems that the activity’s ScreenOrientation enum doesn’t support multiple values. Would setting it to SensorPortrait instead of just Portrait solve this issue? or maybe _graphics.SupportedOrientations shouldnt be set to both DisplayOrientation.Portrait & DisplayOrientation.PortraitDown? I still dont know whats the cause of the problem to begin with…
I am getting tons of these errors reported by BugSense everyday, I don’t want to push an update unless I know I fixed the issue. Problem is I cant replicate it. Any ideas to what the cause is?
I have the supported orientations in LoadContent()
Graphics.SupportedOrientations = DisplayOrientation.Portrait |
DisplayOrientation.PortraitDown |
DisplayOrientation.LandscapeLeft |
DisplayOrientation.LandscapeRight;
The error is an object reference not set error. Here is the stack trace. Any help would be appreciated.
StackTrace: at Microsoft.Xna.Framework.OrientationListener.OnOrientationChanged (System.Int32 orientation) [0x00019] in <8335ffb25c73411d8c018e841c87a942>:0
at Android.Views.OrientationEventListener.n_OnOrientationChanged_I (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 orientation) [0x00009] in <53c60d650862457f9c9b6a8ddcee80ca>:0
at (wrapper dynamic-method) System.Object.13(intptr,intptr,int)
I don’t know how others manage to work with this, I guess you can lock the ScreenOrientation to either Landscape or Portait in activity1.cs and that’s it.
I had an old project on XNA/WindowsPhone and MG/Android 3.4 and when I upgraded to 3.7 I didn’t want to add extra platform specific workarounds since I knew that the code already worked.
Instead I had the original functionally restored for my MG branch. Additionally I added a small delay to make rotation shock-proof and reduce the effective angle from 90 to 45 degrees (with a 45 degree ‘no-change’ gap between orientations).
If you want to use my changes you can find the binaries in my nuGet here.
or apply this commit in your branch.
You also have to change ScreenOrientation = ScreenOrientation.Landscape in activity1.cs.
Based on the code (OnOrientationChanged), this happens before SetDisplayOrientation. I would almost venture to say that users are rotating the screen prior to the Game Window being initialized…