0

I've run into a weird issue with Java for Android. I'm creating a listener for a button and I need to grab a text edit as a string and return it into a string to be used as an authentication check.
I'm getting an error that I need to declare the variable AccessCraw as final and when I create it as a final it says that I can't edit it. How can I preform this without getting a whole bunch of errors? (code below)

    boolean loggedin = (false);
    boolean acsessgranted = (false);
    final String AccessCraw = "no";
    Button submit = (Button) findViewById(R.id.Submit);
    Button startBtn = (Button) findViewById(R.id.EmailAdmin);
    final EditText AccessCode = (EditText) findViewById(R.id.ACentry);

    if(acsessgranted == (false)) {
        setContentView(R.layout.activity_frun);
    }

    if(acsessgranted == (true)) {
        if(loggedin == (false))  {
            setContentView(R.layout.login);
        }
    }

    if(acsessgranted  == (true)) {
        if(loggedin == (true))  {
            cont ();
        }
    }

    startBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            sendEmail();
        }
    });
    submit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AccessCraw = AccessCode.getText().toString();
        }
    });
}
  • what is the purpose of creating a final variable ? – droidev Oct 11 '16 at 04:17
  • 1
    Why are you trying to assign to that variable? The variable will be out of scope before the event triggers. – 4castle Oct 11 '16 at 04:17
  • Declare AccessCraw as global variable, which may solve your problem. – raasesh Oct 11 '16 at 04:20
  • You can read this answer to add your reference http://stackoverflow.com/questions/10233309/does-it-make-sense-to-define-a-final-string-in-java – Indra Nababan Oct 11 '16 at 04:26
  • 1
    I don't think we know enough about your program. You have a listener that sets a variable, but you haven't shown us where in the program you want that variable to be used, or whether there will be just one of that variable or whether there are multiple objects that have an `AccessCraw`. – ajb Oct 11 '16 at 04:27

4 Answers4

0

I think this is what you need to know:

If you don't place EditText in the clas-wide scope, and instead declare it in OnCreate, it wants it to be declared as final.

So declare your EditeText in the clas-wide scope.

SpiralDev
  • 7,011
  • 5
  • 28
  • 42
0

This is very similar to a popular issue in JavaScript. You shouldn't need to assign to global variables from asynchronous events. Instead, call a method which uses the value as a parameter.

Call the code that needs the asynchronous value from inside the event handler.

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106
0

whenever you get such alert, make the variable global.
reason? when you use a variable in innerclass it makes a new copy of that variable and any changes made to it will not be reflected outside. so we declare the variable final and do not make any changes to avoid conflicts, but if there is a need to make changes to that variable, you make it global so that it has same access to all classes and no duplicates are created. read java docs for more details.

And as mentioned in one of the answers, to retain data security, its suggested to use setters and getters which manipulate the variable from inner classes or asynchronous menthods.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
0

Declaring a variable as final is purposely done to prevent its value from being changed in the future. Obviously, this is why you aren't able to change the value.

Android, and java in general, requires variables not declared in the global (class-level) scope to be declared final if you wish to use them in an inner class. This is to prevent confusion if the variable is changed.

As others have mentioned, if you need the String to start with a value, declare it in the global scope.

user2465510
  • 99
  • 10