-2

first of all Happy new year to everyone. I am new to Android and Firebase and have a problem in passing data from one Activity (RegisterActivity) to another (MenuActivity) that contains the DrawerLayout. How can I fix my error? I've tried several solutions but none of them work. This is the error:

RegisterActivity.java

 @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {
                    Toast.makeText(RegisterActivity.this, "User successful created", Toast.LENGTH_SHORT).show();
                    Intent registrationIntent = new Intent(getApplicationContext(), MenuActivity.class);
                    Bundle data = new Bundle();
                    data.putString("EMAIL", email);
                    registrationIntent.putExtras(data);
                    startActivity(registrationIntent);
                    finish();
                    progressDialog.dismiss();
                }
                else {
                    Toast.makeText(RegisterActivity.this, "Error! " +
                            task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

MenuActivity.java

private DrawerLayout drawer;
    private ImageView imageProfile;
    private TextView userName;
    private TextView email;
    private User user;
    private String identification;
    private NavigationView navigationView;

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

        Intent intentReceived = getIntent();
        Bundle data = intentReceived.getExtras();
        if(data != null){
            identification = data.getString("EMAIL");
        }
        else{
            identification = "";
        }

        View navHeaderView = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0);

        ((TextView) navHeaderView.findViewById(R.id.userName)).setText(user.getUserName());
        ((TextView) navHeaderView.findViewById(R.id.userEmail)).setText(user.getEmail());

        Bitmap bitmapsinfoto = BitmapFactory.decodeResource(getResources(), R.drawable.profile);
        RoundedBitmapDrawable roundedBitmapDrawablesinfoto = RoundedBitmapDrawableFactory.create(getResources(), bitmapsinfoto);
        roundedBitmapDrawablesinfoto.setCircular(true);

        ((ImageView) navHeaderView.findViewById(R.id.imageProfile)).setImageDrawable(roundedBitmapDrawablesinfoto);

        if(user.getBytes()!=null) {
            byte[] foodImage = user.getBytes();
            Bitmap bitmap = BitmapFactory.decodeByteArray(foodImage, 0, foodImage.length);

            ((ImageView) navHeaderView.findViewById(R.id.imageProfile)).setImageBitmap(bitmap);

            Bitmap bitmap2 = ((BitmapDrawable)((ImageView) navHeaderView.findViewById(R.id.imageProfile)).getDrawable()).getBitmap();
            RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap2);
            roundedBitmapDrawable.setCircular(true);

            ((ImageView) navHeaderView.findViewById(R.id.imageProfile)).setImageDrawable(roundedBitmapDrawable);
        }

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if(savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new MenuFragment()).commit();
            navigationView.setCheckedItem(R.id.nav_menu);
        }

User.java

public class User {

    private byte[] bytes;
    private String id, email, password, userName;
    private boolean active;

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public User() {  }

    public User(boolean active) {
        this.active = active;
    }

    public User(String id, String email, String password, String userName) {
        this.id = id;
        this.email = email;
        this.password = password;
        this.userName = userName;
    }

    public User(String id, String email, String password, byte[] bytes, String userName) {
        this.bytes = bytes;
        this.id = id;
        this.email = email;
        this.password = password;
        this.userName = userName;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String pass) {
        this.password = pass;
    }

    public String getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Thanks in advance everyone for the answer.

1 Answers1

1

From your code of MenuActivity.java it is somewhat clear that you haven't instantiated the object of User class. For ex,

((TextView) navHeaderView.findViewById(R.id.userName)).setText(user.getUserName());
((TextView) navHeaderView.findViewById(R.id.userEmail)).setText(user.getEmail());

In the above two lines you have used user.getUserName() but you haven't instantiated it before like User user=new User() or anything as you have overloaded the constructor.

Edit: Yours is not showing name and email in drawer because the user.getUserName() and user.getEmail() may be empty. Try passing the values in the constructor with all the required arguments.

Mrudul Tora
  • 715
  • 8
  • 14