I'm trying to access the h
variable in the inner class but an error keeps showing up "Cannot assign a value to final variable h
". I tried quick-fix and it instructed me to "Transform h to final one element array".What does that mean?
int Update ()
{
final int h;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equalsIgnoreCase("PINCODE"))
{
h = sharedPreferences.getInt(key,0);
}
}
});
return h;
}
}
From the inner class you can't assign value to a local variable (itself) declared somewhere in the enclosing class, but you can change state (call methods, setters, ...) of the referenced object (if the variable points to some object and not to a primitive type). And array is object.
Check Java language specification - section about inner classes.
The variable must be assigned as static in class MainActivity.
static int h;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With