title: How to set up an Async task from an activity in Kotlin
body: <p>I have an activity that calls a class and runs a function in that class like so:</p>
  
  <pre><code>        for (i in 0 until questions) {
              droll.droolCalls(sign)
          }
  </code></pre>
  
  <p>This can sometimes run forever as it has to generate a bunch of random numbers, so I want it to be able to run in the background. I wrote an AsyncTask that looks like this:</p>
  
  <pre><code>class MyAsync(
      private val droll: CreateLayout,
      private val questions: Int, private val sign:Int,
      var progressBar: ProgressBar,
      var layoutProgress: LinearLayout,
      var layoutMain: LinearLayout
  ) :
      AsyncTask&lt;String, Int, CreateLayout&gt;() {
  
  
      var progress = 0
      override fun doInBackground(vararg params: String?): CreateLayout {
          for (i in 0 until questions) {
              droll.droolCalls(sign)
          }
          return droll
      }
  
      override fun onProgressUpdate(vararg values: Int?) {
          super.onProgressUpdate(*values)
          progressBar.incrementProgressBy(progress * 10)
      }
  
      override fun onPostExecute(result: CreateLayout?) {
          super.onPostExecute(result)
          layoutProgress.visibility = View.GONE
          layoutMain.visibility = View.VISIBLE
      }
  
  }
  </code></pre>
  
  <p>However, when i call the drollclass </p>
  
  <p><code>MyAsync(droll, totalQuestions,sign, progressBar, Loading, mainLayout).execute("hello")</code></p>
  
  <p>i get an error from other functions that require drool to run.</p>
  
  <p>this one for example <code>insert_point.addView(droll.makeTextView(numberOfQuestions - 1))</code> gives me a <code>java.lang.IndexOutOfBoundsException: Index: 2, Size: 0</code> error because insert_point not getting the data from droll because the Async didn't run? however if i take it out of the Async the for loop our of the Async it works fine.</p>
  
  <p>the whole structure looks something like this</p>
  
  <pre><code>class mainclass{
  
  MyAsync(droll, totalQuestions,sign, progressBar, Loading, mainLayout).execute("hello")
  insert_point.addView(droll.makeTextView(numberOfQuestions - 1))
  
  
  class MyAsync(
      private val droll: CreateLayout,
      private val questions: Int, private val sign:Int,
      var progressBar: ProgressBar,
      var layoutProgress: LinearLayout,
      var layoutMain: LinearLayout
  ) :
      AsyncTask&lt;String, Int, CreateLayout&gt;() {
  
  
      var progress = 0
      override fun doInBackground(vararg params: String?): CreateLayout {
          for (i in 0 until questions) {
              droll.droolCalls(sign)
          }
          return droll
      }
  
      override fun onProgressUpdate(vararg values: Int?) {
          super.onProgressUpdate(*values)
          progressBar.incrementProgressBy(progress * 10)
      }
  
      override fun onPostExecute(result: CreateLayout?) {
          super.onPostExecute(result)
          layoutProgress.visibility = View.GONE
          layoutMain.visibility = View.VISIBLE
      }
  
  }
  </code></pre>
  
  <p>}</p>