Android: UrhoSurface.RunInActivity( ) doesn't work with [SimpleApplication]

Hi!

Assume the below call from Android application's OnCreate( ) method:

UrhoSurface.RunInActivity<MyApplication>(new ApplicationOptions("Data"));

Where MyApplication is defined following way:

internal class MyApplication : Urho.SimpleApplication
{
    public MyApplication(ApplicationOptions options) : base(options)
    {
        // init the app here
    }
}

The above causes following runtime exception:

04-11 15:45:27.218 E/mono (13551): Unhandled Exception:
04-11 15:45:27.218 E/mono (13551): System.InvalidOperationException: Constructor UrhoSimpleAppTest.MyApplication(ApplicationOptions) was not found. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object

But the official documentation says:

The simplest way to run a game, as implemented in the MyGame class is to call

UrhoSurface.RunInActivity();

Any idea why it doesn't work?
In my project the SimpleApplication class should be enough, that's why I am trying the above.

BR,

Ladislav

Best Answer

  • lahellerlaheller US ✭✭
    edited April 2018 Accepted Answer

    Finally I solved it, here is how:

            SimpleApplication app = null;
            bool IsStarted = false;
    
            protected override void OnCreate(Bundle bundle) {
                base.OnCreate(bundle);
    
                SimpleApplication.Started += () => {
                    SysTrace.WriteLine("[ === Application started! === ]");
                    IsStarted = true;
                };
    
                SimpleApplication.Stopped += () => {
                    SysTrace.WriteLine("[ === Application stopped! === ]");
                    Finish();
                };
    
                SimpleApplication.UnhandledException += (s1, e1) => {
                    SysTrace.WriteLine("SimpleApplication >> " + e1.Exception.Message);
                    e1.Handled = true;
                };
    
                var options = new ApplicationOptions("Data") { Orientation = ApplicationOptions.OrientationType.LandscapeAndPortrait };
                UrhoSurface.RunInActivity(() => {
                    app = new SimpleApplication(options);
                    return app;
                }, options);
    
                ThreadPool.QueueUserWorkItem((arg1) => {
                    while (!IsStarted) { }
                    SimpleApplication.InvokeOnMain(() => {
                        var boxNode = app.RootNode.CreateChild("boxNode");
                        boxNode.Position = new Vector3(0, 0, 3);
    
                        var box = boxNode.CreateComponent<Box>();
                        box.Color = Color.Gray;
                    });
                });
            }
    

Answers

  • lahellerlaheller USMember ✭✭
    edited April 2018 Accepted Answer

    Finally I solved it, here is how:

            SimpleApplication app = null;
            bool IsStarted = false;
    
            protected override void OnCreate(Bundle bundle) {
                base.OnCreate(bundle);
    
                SimpleApplication.Started += () => {
                    SysTrace.WriteLine("[ === Application started! === ]");
                    IsStarted = true;
                };
    
                SimpleApplication.Stopped += () => {
                    SysTrace.WriteLine("[ === Application stopped! === ]");
                    Finish();
                };
    
                SimpleApplication.UnhandledException += (s1, e1) => {
                    SysTrace.WriteLine("SimpleApplication >> " + e1.Exception.Message);
                    e1.Handled = true;
                };
    
                var options = new ApplicationOptions("Data") { Orientation = ApplicationOptions.OrientationType.LandscapeAndPortrait };
                UrhoSurface.RunInActivity(() => {
                    app = new SimpleApplication(options);
                    return app;
                }, options);
    
                ThreadPool.QueueUserWorkItem((arg1) => {
                    while (!IsStarted) { }
                    SimpleApplication.InvokeOnMain(() => {
                        var boxNode = app.RootNode.CreateChild("boxNode");
                        boxNode.Position = new Vector3(0, 0, 3);
    
                        var box = boxNode.CreateComponent<Box>();
                        box.Color = Color.Gray;
                    });
                });
            }
    
Sign In or Register to comment.