title: Android Studio: initializing shared preferences crashing activity launch
body: <p>My android app is crashing at activity launch from the main activity, but only when I initialize a SharedPreferences object.</p>
  <p>In Main Activity:</p>
  <pre><code>package my.investing.investingtestapp
  
  import android.content.Intent
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import kotlinx.android.synthetic.main.activity_main.*
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          btnPlay.setOnClickListener {
              // Handler code here.
              val I = Intent(this, PlayActivity::class.java)
              startActivity(I);
          }
      }
  }
  </code></pre>
  <p>In Play Activity:</p>
  <pre><code>package my.company.example
  
  import android.content.Context
  import android.content.SharedPreferences
  import android.content.SharedPreferences.Editor
  import android.os.Bundle
  import androidx.appcompat.app.AppCompatActivity
  
  class PlayActivity : AppCompatActivity() {
  
      val prefs: SharedPreferences? by lazy { this.getPreferences(Context.MODE_PRIVATE) }
      val editor: Editor = prefs!!.edit()
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_play2)
      }
  }
  </code></pre>
  <p>This is true also for:</p>
  <pre><code>val prefs = this.getPreferences(Context.MODE_PRIVATE)
  </code></pre>
  <p>error:
  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.company.example/my.company.example.PlayActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
  at android.os.Handler.dispatchMessage(Handler.java:106)
  at android.os.Looper.loop(Looper.java:223)
  at android.app.ActivityThread.main(ActivityThread.java:7656)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:149)
  at android.app.Activity.getLocalClassName(Activity.java:6738)
  at android.app.Activity.getPreferences(Activity.java:6782)
  at my.company.example.PlayActivity$prefs$2.invoke(PlayActivity.kt:39)
  at my.company.example.PlayActivity$prefs$2.invoke(PlayActivity.kt:37)
  at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
  at my.company.example.PlayActivity.getPrefs(Unknown Source:2)
  at my.company.example.PlayActivity.(PlayActivity.kt:40)
  at java.lang.Class.newInstance(Native Method)
  at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
  at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
  at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
  at android.os.Handler.dispatchMessage(Handler.java:106) 
  at android.os.Looper.loop(Looper.java:223) 
  at android.app.ActivityThread.main(ActivityThread.java:7656) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) </p>