Xander Xander - 3 months ago 24
Android Question

firebase tutorial-modifier private is not allowed here

I'm attempting to add login functionality to my android app using Firebase. I'm following the instructions at https://firebase.google.com/docs/auth/android/password-auth, but when I attempt to use the private modifier, Android Studio tells me it's "not allowed here." But if I don't use that modifier, I can't call mAuth in onStart.

Here is my code:

package com.priceelectric.xriley.priceprefab;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class Login extends AppCompatActivity {

private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

private FirebaseAuth mAuth;

mAuth = FirebaseAuth.getInstance();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null){
Log.d("loginTag", "onAuthStateChanged:signed_in:" + user.getUid());
}
else{
Log.d("loginTag", "onAuthStateChanged:signed_out");
}

}
};
}

@Override
public void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
}

Answer

That's basic Java rule. You cannot use private, public, protected keywords inside methods. You can only set the visibility of a "global" variable as you did for private FirebaseAuth.AuthStateListener mAuthListener;.

Solution: Put private FirebaseAuth mAuth; right below private FirebaseAuth.AuthStateListener mAuthListener;.