I was trying something and stumbled upon a problem where I can't access a global variable str1
from a class MyAsyncTask
. I'm guessing, its got to be some declaration error but I'm not too sure as to as to what is being violated.
class MainActivity : AppCompatActivity() {
var str1 = "0"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for (i in 0..5){
val myAsyncTask = MyAsyncTask(this@MainActivity)
myAsyncTask.execute("-")
str1 = str1 + "1"
}
}
class MyAsyncTask(private var c : Context):AsyncTask<String,Void,String>(){
override fun doInBackground(vararg str: String?): String {
str1 = str1 + str[0] + "2" // str1 is bolded in red, somehow, i cant access it
return str1
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
Toast.makeText(c, result, Toast.LENGTH_SHORT).show()
}
}
}