2

In the docs, it says I should make the new class like this:

class MyView extends GLSurfaceView {
    public MyView(Context context) {
        super(context);
        setRenderer(renderer);
    }
}

Now I tried to re-do that in Scala:

class BaseGameActivity extends Activity {
    object glview extends GLSurfaceView(this) {
        setRenderer(renderer)
        setEGLContextClientVersion(2)
    }
}

However, the App crashes now with the exception "java.lang.IllegalStateException: setRenderer already called for this instance". I suspect this has to do with the way Scala calls the super-constructor.

I've tried to find out how to override the constructor in the way the docs describe, but couldn't find it. I'd appreciate any hint.

Lanbo
  • 15,118
  • 16
  • 70
  • 147
  • 4
    There's no such thing as overriding constructors. The only kind of polymorphism that constructors support is overloading. – Randall Schulz Apr 21 '13 at 18:34

2 Answers2

2

It seems to me that your are propagating the call to a different constructor from the base class. You are passing a reference to this instead of a reference to the Context object. It might be that this other constructor is calling setRenderer.

Could you try to create an inner class MyGLView like this:

class MyGLView(ctx: Context) extends GLSurfaceView(ctx) {
  setRenderer(renderer)
}

And see what happens?

The problem is that object does not allow arguments to its constructor. Top-level objects must be initializable without any arguments (nobody calls their ctors). In your case you have an inner object, which can reference the members of the surrounding class instance. If you really need an inner object in your Activity class, you could do:

object glview extends GLSurfaceView(ctx) {
  setRenderer(renderer)
}

where ctx is a member of the surrounding class.

axel22
  • 32,045
  • 9
  • 125
  • 137
0

In java likewise in scala constructors are not inherited.

So you can not override thing, you didnt inherit. And you should use one of existing constructors for base class. If all of them are calling setRenderer(renderer) it will be called during constructing super object and you obviously should not call it second time in a subtype constructor ( wheither it class, object or mixing-in trait ).

Community
  • 1
  • 1
Odomontois
  • 15,918
  • 2
  • 36
  • 71