0

The code that I want to convert is the following;

public class AndroidLauncher extends AndroidApplication {

   public static AndroidLauncher androidLauncher;
   @Override
   protected void onCreate (Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       androidLauncher = this;
   }
}

What android studio generated code is this;

class AndroidLauncher : AndroidApplication() {
   protected override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       androidLauncher = this
   }

   companion object {

       var androidLauncher: AndroidLauncher
   }
}

This code gives me error, which is;

Property must be initialized or be abstract

I'm developing a game with libgdx, so I'll use this approach to use the Game object from anywhere I want. It's a singleton class so it won't leak any memory.

Efe Budak
  • 659
  • 5
  • 27
  • `AndroidLauncher` is an `Activity` – tynn Dec 05 '17 at 23:21
  • 2
    It does not matter. However, keeping a static variable of an activity would leak memory which I'm not planning on. – Efe Budak Dec 05 '17 at 23:25
  • @tynn `AndroidLauncher` is an `Application`, not an `Activity`... – m0skit0 Dec 06 '17 at 11:05
  • @m0skit0 it's actually both in different domains https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/backends/android/AndroidApplication.html – tynn Dec 06 '17 at 11:13

1 Answers1

1

Use lateinit to indicate that the field will be initialized later.

   companion object {
       lateinit var androidLauncher: AndroidLauncher
   }
m0skit0
  • 25,268
  • 11
  • 79
  • 127