So, My app crashes on 3 other devices but it works fine on the device I have been using since the beginning of the development process. When I checked the LogCat, I got this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.......MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
java.lang.String
com.parse.ParseUser.getUsername() on a null object reference
I assume it is because of this code:
//get current user
ParseUser currentUser = ParseUser.getCurrentUser();
//get current user username and turn it to string
final String currentUserUsername = currentUser.getUsername();
//identify if current user is logged in
if (currentUser != null) {
// bring user to homepage and do stuff with the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("My");
query.orderByDescending("createdAt");
query.whereEqualTo("user", currentUserUsername);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> personObject, ParseException e) {
if(e == null){
//success
Toast.makeText(MainActivity.this, "Welcome Back, " + currentUserUsername + "!", Toast.LENGTH_SHORT).show();
mPerson = peopleObject;
MyPeopleAdapter adapter = new MyPeopleAdapter(getListView().getContext(), mPerson);
setListAdapter(adapter);
} else {
//problem
Toast.makeText(MainActivity.this, "There is a problem. Please try again later.", Toast.LENGTH_SHORT).show();
}
}
});
} else {
// show the signup or login screen
Intent SignIn = new Intent(this, SignInActivity.class);
startActivity(SignIn);
}
But, how can this be a problem? I have been logging in and out of the app with the first device over and over again without any problem. And why is it causing problem with the other devices?
UPDATE:
I just found out if I remove the code, the app works just fine. But it doesn't work when I have the code.
You can avoid the crash by remove the problematic line from outside the Null checking and insert it to the if loop::
//identify if current user is logged in
if (currentUser != null) {
//get current user username and turn it to string
final String currentUserUsername = currentUser.getUsername(); //this line was outside earlier
//rest of code
}
Related
When an email verified user logs in for the first time in my app everything works as expected (he gets to a welcome screen and then to the MainActivity) but when he logs out out and logs in again there is a null pointer exception. When a user logs in for the second time he should go straight to the MainActivity and not to the welcome screen which you see in the below code.
This was a known issue about a year ago so my question is how to resolve this issue? Has this bug been fixed and when yes what did I do wrong in my code?
The question I am referring is here :Firebase user returns null metadata for already signed up users
Here is the code I am using to check whether the user logs in for the first or second time:
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = auth.getCurrentUser();
if (firebaseUser != null && firebaseUser.isEmailVerified()) {
startActivity(new Intent(LoginEmailActivity.this, MainActivity.class));
finish();
}
setContentView(R.layout.activity_email_login);
...
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
progressBar.setVisibility(View.VISIBLE);
final FirebaseUserMetadata metadata = auth.getCurrentUser().getMetadata();
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginEmailActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(LoginEmailActivity.this, "Ups, es ist wohl etwas schief gelaufen. Bitte überprüfe deine Internetverbindung und versuche es erneut.", Toast.LENGTH_LONG).show();
} else {
if (auth.getCurrentUser().isEmailVerified() && !(metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())){
Intent intent = new Intent(LoginEmailActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else if (auth.getCurrentUser().isEmailVerified() && (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())){
firstLoginScreen();
finish();
}
else if (!auth.getCurrentUser().isEmailVerified()){
Toast.makeText(LoginEmailActivity.this, "Bitte verifiziere erst deine E-Mail Adresse mit dem Link, den wir dir geschickt haben.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
});
...
Here is the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUserMetadata com.google.firebase.auth.FirebaseUser.getMetadata()' on a null object reference
at com.example.android.guessit.LoginRegistration.LoginEmailActivity$1.onClick
Also the "getCreationTimestamp" says that it may produce a NullPointerException
The problem is where you call
final FirebaseUserMetadata metadata = auth.getCurrentUser().getMetadata()
Calling it before completing the user login, brings you to the NullPointer you are facing. Just move it inside the onComplete callback, where the current user has a value.
Also, just for code optimisation add a return statement when you switch to the MainActivity.
if (firebaseUser != null && firebaseUser.isEmailVerified()) {
startActivity(new Intent(LoginEmailActivity.this, MainActivity.class));
finish();
return;
}
You need a return in your if statement when you go to the MainActivity. Otherwise after that you set again setContentView(R.layout.activity_email_login).
I have the following code that allows users to update their data:
String username = "Any username that is currently already used by another user";
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
currentUser.setUsername(username);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// All good!
} else {
// Error: Account already exists for this username.
Log.e("Error: ", e.getMessage());
}
}
}
});
When the user is trying to update the username with one that is already used by someone else, it throws an error:
Account already exists for this username.
Which is exactly what I'd want it to, but when the user goes back (without making another request to change the username to one that is available) the ParseUser.get("username") returns the value that wasn't saved because it already exists (instead of the real value stored at the moment in the server).
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.fetchInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException exception) {
if (exception == null) {
// username now returns ("Any username that is currently already used by another user").
String username = user.get("username").toString().trim();
} else {
// Error
}
}
});
The only way I've found so far to fix the issue is by uninstalling/reinstalling the app. What's causing this? And is there any good way to fix this?
You should backup the current username before setUsername(newName).
if exception happened you have two options either :
Re fetch the user object by ParseUser.getCurrentUser().fetch() (to restore the object to his previous state)
Re set the username to the previous value(that you already back up before)
Why this happened ?
because when you called the setUsername() of the user object you changed your local copy of the object (but you didn't sync it yet with the server) and then when you made save() and the save operation failed the local copy still has the latest changes that you made locally
When the user clicks on the "find random opponent" button, I would like the User class in Parse to create a new field (called readyToPlay) and set the value to true. Then, start a new activity where the user is randomly matched with other users whose variable readyToPlay is also set to true.
The problem is when I click the "find random opponent" button, no new field is created. I looked at the Parse documentation and cant figure out what I am doing wrong. I have attached the relevant code below.
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FindRandomOpponent.class);
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put(ParseConstants.KEY_READY_TO_PLAY, true);
currentUser.saveInBackground()
startActivity(intent);
}
Edit1: I called the saveInBackground method, but my user database didn't update.
Edit2: I am currently signed in as a user, so the device does have access to the internet.
This will help. It will check your user is saved or give exception for further investigation.
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put(ParseConstants.KEY_READY_TO_PLAY, true);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Intent intent = new Intent(MainActivity.this, FindRandomOpponent.class);
startActivity(intent);
} else {
//log error
}
}
});
You need to call the saveInBackground() method on your currentUser object.
Reference: Parse Docs on saving objects
I'm currently building an app that has Parse at its backend. I've realized that a user can log in to his account in multiple devices. Is there a way I could restrict this to only one device at one time? If so can you kindly explain the method.
Thanks in advance.
you need to monitor the application will your web environment (webservice for example) As soon as someone you login you must disconnect the other devices connected to the same user.
You can analyze it by IMEI who made the last login request and send a command to the other devices of the same user to remove access.
I came here looking for a solution and didn't find one. Through trial and error, I have solved it. I am a novice developer and do not know if this is the best practice, but it works. When the user attempts to log in execute the following.
public void login(View v) {
// Create string variables for data.
String username = et_username.getText().toString().toLowerCase().trim();
String password = et_password.getText().toString();
// If a user is already on this device, log them out.
// this will happen when the force log out happens, the device will
// simply catch the invalid session, but still have that user data, just unavailable
user = ParseUser.getCurrentUser();
if (user != null)
user.logOutInBackground();
ParseUser.logInInBackground(username, password, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
final ParseQuery<ParseObject> query = new ParseQuery<>("_Session");
query.include("user");
query.whereEqualTo("user", ParseUser.getCurrentUser()); // Only get sessions for the specific user.
query.addAscendingOrder("createdAt"); // Place them in chronological order, with the oldest at the top.
query.countInBackground(new CountCallback() {
#Override
public void done(int count, ParseException e) {
if (count > 1) {
try {
query.getFirst().deleteInBackground();
} catch (ParseException e1) {
Toast.makeText(LoginActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
}
}
}
});
if (user != null) {
emailVerified = user.getBoolean("emailVerified");
if (emailVerified) {
// Update the database to track their log in.
user.put("loggedIn", true);
user.saveInBackground();
if (!deliquent) {
// Launch the MainActivity.
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
else {
Intent intent = new Intent(LoginActivity.this, CardActivity.class);
startActivity(intent);
}
}
else {
Toast.makeText(LoginActivity.this, getResources().getString(R.string.verify), Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(LoginActivity.this, "From LoginActivity line:213\n" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
I'm using Firebase SimpleLogin to enable Email / Password authentication. Creation of users and subsequent login is all working fine. However, whenever I leave the app (even if only for a few seconds) the user is never logged in on my return i.e...
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...
Always returns a null user.
I am not logging out the user via the API. Also I have set the number of days the user is logged in to 21 in the Firebase console.
I have seen mention of a remember-me param in the JS docs, but I can't see any equivalent for Android / Java.
Wondering if I'm missing anything in the docs or if it's not possible for Android?
Thanks for your help,
Neil.
Edit: Added code sample.
User creation....
public void registerUserForChat(final MyApplication application, String email, String password) {
Firebase ref = new Firebase(FIREBASE_URL);
SimpleLogin authClient = new SimpleLogin(ref);
authClient.createUser(email, password, new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
Log.e(TAG, "Error attempting to create new Firebase User: " + error);
}
else {
Log.d(TAG, "User successfully registered for Firebase");
application.setLoggedIntoChat(true);
}
}
});
}
User login....
public void loginUserForChat(final MyApplication application, String email, String password) {
Log.d(TAG, "Attempting to login Firebase user...");
Firebase ref = new Firebase(FirebaseService.FIREBASE_URL);
final SimpleLogin authClient = new SimpleLogin(ref);
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if (error != null) {
Log.d(TAG, "error performing check: " + error);
} else if (user == null) {
Log.d(TAG, "no user logged in. Will login...");
authClient.loginWithEmail(email, password, new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
if(com.firebase.simplelogin.enums.Error.UserDoesNotExist == error) {
Log.e(TAG, "UserDoesNotExist!");
} else {
Log.e(TAG, "Error attempting to login Firebase User: " + error);
}
}
else {
Log.d(TAG, "User successfully logged into Firebase");
application.setLoggedIntoChat(true);
}
}
});
} else {
Log.d(TAG, "user is logged in");
}
}
});
}
So loginUserForChat method first checks to see if there is a logged in user and, if not, performs the login. Note that every time I start the app, the logging I see is....
Attempting to login Firebase user...
no user logged in. Will login...
User successfully logged into Firebase
If I exit the app, even for a few seconds, and return - I see the same logging.
One thing I noticed is that the call to checkAuthStatus does not take any user credentials - I assume it just checks for any locally logged in user?
Much appreciated.
Another way - try this code in your onCreate:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
This will keep the user logged in by taking the user to the Main activity directly without stopping at registration activity. so the user will be logged in unless the user click on signout.
[Engineer at Firebase] In order to transparently handle persistent sessions in the Firebase Simple Login Java client, you need to use the two-argument constructor which accepts an Android context, i.e. SimpleLogin(com.firebase.client.Firebase ref, android.content.Context context) every time you instantiate the Simple Login Java client.
See https://www.firebase.com/docs/java-simple-login-api/javadoc/com/firebase/simplelogin/SimpleLogin.html for the full API reference.
The proper way to do it is to use oAuth authentication:
1. The user logs in.
2. You generate an access token(oAuth2).
3. Android app saves the token locally.
4. Each time the comes back to the auth, he can use the token to to log in, unless the token has been revoked by you, or he changed his
password.
Luckily, firebase has an out of the box support for that, docs:
https://www.firebase.com/docs/security/custom-login.html
https://www.firebase.com/docs/security/authentication.html
You can do this by Using this Approach to escape logi page if User already logged in.
private FirebaseAuth auth;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(Login_Activity.this, Home.class));
finish();
}
setContentView(R.layout.activity_login_);
for those using Kotlin, to keep the user logged in just add in the onCreate function
if (auth.currentUser != null)
{
startActivity(Intent(this#Login, SellingPageHolderActivity::class.java))
finish()
}