-2
package com.example.firebase_project;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
 import android.view.View;
 import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {
private   FirebaseUser currentuser;
private   Button loginbutton,PhoneLoginButton;
private  TextView forgetPasswordLink,NeedNewAccountLink;
private  EditText UserEmail,UserPassword;
 private FirebaseAuth mAuth;
 private ProgressDialog loadingBar;

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

    currentuser=mAuth.getCurrentUser();
    mAuth=FirebaseAuth.getInstance();
    NeedNewAccountLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SendUserToRegisterActivity();
        }
    });
    loginbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AllowUserToLogin();
        }
    });
}

private void AllowUserToLogin() {
    String Email=UserEmail.getText().toString();
    String Password=UserPassword.getText().toString();
    if(TextUtils.isEmpty(Email))
    {
        Toast.makeText(this,"plz enter email",Toast.LENGTH_SHORT).show();
    }
    if(TextUtils.isEmpty(Password))
    {
        Toast.makeText(this,"plz enter password",Toast.LENGTH_SHORT).show();
    }
    else{
        loadingBar.setTitle("Log in");
        loadingBar.setMessage("plz wait");
        loadingBar.setCanceledOnTouchOutside(true);
        mAuth.signInWithEmailAndPassword(Email,Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful())
                {
                    sendtoMainActivity();
                    Toast.makeText(LoginActivity.this,"Login success ful",Toast.LENGTH_SHORT).show();
                    loadingBar.dismiss();
                }
                else {
                    String massage = task.getException().toString();
                    Toast.makeText(LoginActivity.this, massage, Toast.LENGTH_SHORT).show();
                    loadingBar.dismiss();

                }

                }
        });

    }
}

private void sendtoMainActivity() {
    Intent MainIntent=new Intent(LoginActivity.this,MainActivity.class);
    startActivity(MainIntent);

}

protected  void InatlizeField()
{
   loginbutton=(Button)findViewById(R.id.login_button);
   PhoneLoginButton=(Button)findViewById(R.id.phone_login);
   forgetPasswordLink=(TextView)findViewById(R.id.forget_password);
   NeedNewAccountLink=(TextView)findViewById(R.id.create_account);
   UserEmail=(EditText)findViewById(R.id.login_email);
   UserPassword=(EditText)findViewById(R.id.login_password);
    loadingBar=new ProgressDialog(this);
}

@Override
protected void onStart() {
    super.onStart();
    if(currentuser !=null)
    {
        SendUserToMainActivity();
    }
}
protected void SendUserToMainActivity()
{
    Intent MainActivityIntent=new Intent(LoginActivity.this,MainActivity.class);
    startActivity(MainActivityIntent);
}
protected void SendUserToRegisterActivity()
{
    Intent RegisterIntent=new Intent(LoginActivity.this,RegisterActivity.class);
    startActivity(RegisterIntent);
}

}

MainActivity.java

package com.example.firebase_project;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private TabAcessAdaptor tabAcessAdaptor;
private FirebaseUser currentuser;
private FirebaseAuth mAuth;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAuth = FirebaseAuth.getInstance();
    currentuser=mAuth.getCurrentUser();


    toolbar=(Toolbar)findViewById(R.id.main_page_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Created By Sumit Kumar");


    viewPager=(ViewPager)findViewById(R.id.main_tab_paper);
    tabAcessAdaptor=new TabAcessAdaptor(getSupportFragmentManager());
    viewPager.setAdapter(tabAcessAdaptor);

    tabLayout=(TabLayout)findViewById(R.id.main_tabs);
    tabLayout.setupWithViewPager(viewPager);

}

@Override
protected void onStart() {
    super.onStart();
    if(currentuser ==null)
    {
        SendUserToLoginPge();
    }
}
private void SendUserToLoginPge()
{
    Intent LoginIntent=new Intent(MainActivity.this,LoginActivity.class);
    startActivity(LoginIntent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.option_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    super.onOptionsItemSelected(item);
    if (item.getItemId()==R.id.main_find_friend_option)
    {

    }
    if (item.getItemId()==R.id.main_setting_option)
    {

    }
    if (item.getItemId()==R.id.main_logout_option)
    {
        mAuth.signOut();
        sendUserToLoginPge();
    }
    return  true;
}

private void sendUserToLoginPge() {
    Intent intent =new Intent(MainActivity.this,LoginActivity.class);
    startActivity(intent);
}

}

when i creating login and signup a with firebase authantification

getting error ** Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference at com.example.firebase_project.LoginActivity.onCreate(LoginActivity.java:35)**

private FirebaseUser currentuser; currentuser=mAuth.getCurrentUser();

i dont know how It show nullPointerexception

  • Where did you assign `mAuth`? – Doug Stevenson Apr 19 '19 at 16:48
  • public class LoginActivity extends AppCompatActivity { private FirebaseUser currentuser; private Button loginbutton,PhoneLoginButton; private TextView forgetPasswordLink,NeedNewAccountLink; private EditText UserEmail,UserPassword; private FirebaseAuth mAuth; private ProgressDialog loadingBar; – Sumit kumar Apr 19 '19 at 17:26
  • i asign mAuth in LoginActivty above onCreate – Sumit kumar Apr 19 '19 at 17:28
  • Maybe you could edit the question to show all the relevant code? – Doug Stevenson Apr 19 '19 at 17:56
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 19 '19 at 18:09

1 Answers1

1

You assigned mAuth in MainActivity, but not in LoginActivity. If you want to use it in LoginActivity, you'll have to assign it there as well.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302