K
K
Kostya Bakay2016-01-05 19:15:47
Java
Kostya Bakay, 2016-01-05 19:15:47

Why is a public variable not visible in another class?

I have a MainActivity class and I made an inner class in it to use AsyncTask. I decided to scatter these classes into different packages. But after that, my variables were no longer visible in the class for AsyncTask, although they are global and public. Well, for example:
In the MainActivity class, I have a field in front of all methods

public boolean isNavigationDrawerItemEnabled = false;

In my AsyncTask in another package, this variable is no longer seen here
@Override
    protected void onPreExecute() {
        super.onPreExecute();

        if (!isNavigationDrawerItemEnabled) {
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("Новые");
            progressDialog.setMessage("Загрузка...");
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        }
    }

It seems to be banal, but for some reason I blunted. Can you suggest?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2016-01-05
@kostyabakay

There are no global variables, you need to access through a reference to an instance of the class, or to the class itself, if the variable is static. Example with class instance:

package p1

public class one {
 public boolean MyVar;
}

package p2
import p1.one

public class main{

class_one = new one();
class_one.myVar=true;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question