Why can't I access a global variable from AsyncTask?

Rajdeep

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()
        }
    }
}
Zoe

As a general rule, if you have something in Kotlin that, with a similar syntax, would work in Java, decompile the Kotlin code. In IntelliJ/Android Studio, Tools -> Kotlin -> Show Kotlin bytecode. With a slight edit so the code compiles, you'll see the declaration for the class is public static final class MyAsyncTask extends AsyncTask. If you're familiar with how inner classes work, you'll know that static inner classes are like declaring them in a separate file; they do not have context related to their outer class, and can therefore not access non-static variables. It's kinda like accessing a non-static variable from a companion object or a static method. Static anything isn't attached to a specific instance of an object, and can therefore not access non-static variables.

That is the problem. If you google what you want from here, you can usually find links to documentation, at least for basic language-related stuff.

Kotlin has a special keyword you have to use if you want regular inner classes. The keyword is pretty straight-forward too: it's inner. Change your class declaration to inner class MyAsyncTask, decompile, and see how it changed to public final class MyAsyncTask extends AsyncTask.

TL;DR:

This in Java:

public class MyClass {
    public class Something {}
}

Is this in Kotlin:

class MyClass {
    inner class Something 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I access this global variable?

Why can't i access the value in the global variable

Why can't I access a declared variable in the global scope

Why I can't access my script variable from class?

Can't access global variable from other files in Node JS

Why can't I access a global property inside the ngOnInit Method?

Why can't I set a global variable in Python?

why can't I declare a global variable inside an "exec" string

Why I can't use asyncTask

Why i can't access my variable declared in package from sub declared in the same package in Perl?

Why I can't access to Tss variable declared in assembly from this C code?

Why can't I access a variable that's being returned from a function?

Why can't I access an class variable defined in __init__ from outside of the class?

LLVM IR can't access global variable

jquery global variable can not access from outside

Why can't I access my objects member variable?

Why can't I access variable inside a nested function?

Why can't I access localStorage in a string variable on android studio?

Why can't I access variable in my dictionary of JSON object?

why can't I access the variable in my class. python

Why can't I access my variable in the widget?

Why can't i access variable inside if loops in php

Why is this variable not global (can't use in a function)?

Why can I access a component variable only from outside the component?

Why can I access a private variable from main method?

Why I can get access to variable from multiple Tasks in Actor?

Why can't this global function access my global object?

AsyncTask - Why can I access layout widgets in the doInBackground method?

Why can't I call a method after destructuring a variable but I can if I access the field directly?