firebaseAuth.logout workf for menu activity but not for profile activity

1

I may entangled myself in this but I can't seem to see any reason why this doesn't work.

In the MenuActivity the menu items and their functionalities work fine. Log out works perfectly. When I extent the MenuActivity to the ProfileActivity, for some reason the log out button doesn't work the same, the app crashes and displays the message "My application has stopped xClose the application".

Here are all the classes:

MenuActivity.java:

package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;

public class MenuActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();
    }

    private void Logout(){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(MenuActivity.this, MainActivity.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflate = getMenuInflater();
        inflate.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.logoutMenu:{
                Logout();
                return true;
            }
            case R.id.profileMenu:
                startActivity(new Intent(MenuActivity.this, ProfileActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

}

ProfileActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class ProfileActivity extends MenuActivity {

    private TextView userName, userEmail, phoneNumber;
    private Button editProfile, btnChangePassword;
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        userName = findViewById(R.id.tvName3);
        userEmail = findViewById(R.id.tvEmail3);
        phoneNumber = findViewById(R.id.tvPhone3);
        editProfile = findViewById(R.id.btnEditProfile);
        btnChangePassword = findViewById(R.id.btnChangePassword);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        final DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
                userName.setText("Name: " + userProfile.getUserName());
                userEmail.setText("Email: " + userProfile.getUserEmail());
                phoneNumber.setText("Phone: " + userProfile.getUserPhone());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProfileActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT);
            }
        });

        editProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, UpdateProfile.class));
            }
        });
        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, ChangePassword.class));
            }
        });
    }
}

and the menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    android:id="@+id/main_menu" >
    <item
        android:id="@+id/logoutMenu"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:title="Logout">
    </item>

    <item
        android:id="@+id/profileMenu"
        android:orderInCategory="200"
        app:showAsAction="never"
        android:title="Profile">
    </item>

    <item
        android:id="@+id/refreshMenu"
        android:orderInCategory="300"
        app:showAsAction="never"
        android:title="Refresh">
    </item>
</menu>

Here are the logcat messages

2019-05-31 09:15:56.742 20660-20660/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 20660
    android.content.res.Resources$NotFoundException: String resource ID #0xfffffffd
        at android.content.res.Resources.getText(Resources.java:338)
        at android.widget.Toast.makeText(Toast.java:304)
        at com.example.myapplication.ProfileActivity$1.onCancelled(ProfileActivity.java:58)
        at com.google.firebase.database.core.ValueEventRegistration.fireCancelEvent(com.google.firebase:firebase-database@@17.0.0:80)
        at com.google.firebase.database.core.view.CancelEvent.fire(com.google.firebase:firebase-database@@17.0.0:40)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6585)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Is there something I did wrong in the ProfileActivity that interferes with the menu?

android
firebase
firebase-realtime-database
firebase-authentication
asked on Stack Overflow May 30, 2019 by C. Martin • edited May 31, 2019 by C. Martin

3 Answers

0
  private void Logout(){
    firebaseAuth.signOut();
    startActivity(new Intent(MenuActivity.this, MainActivity.class));
}

try this logout function

answered on Stack Overflow May 30, 2019 by Ankush Singh
0

The issue maybe is in the lines that tell the MenuActivity what to do when log out is pressed.

private void Logout(){
    firebaseAuth.signOut();
    finish();
    startActivity(new Intent(MenuActivity.this, MainActivity.class));
}

Tell me if i understood well, the crash happens only when you're in ProfileActivity? Because here you're launching an Intent that pass from MenuActivity to MainActivity, so it may cause problems if you're in ProfileActivity...

0

According to me, I think you should add a check if there is a current user already logged in or not if the user is not logged in it will throw a null pointer exception. Add a check like this

if(FirebaseAuth.getCurrentUser()!=null){
   //Your sign out code
      }
answered on Stack Overflow May 30, 2019 by Aryan Soni

User contributions licensed under CC BY-SA 3.0