I am using an app where initially , there is a intro screen which runs only when the app is started for the first time. At the last slide , I have two buttons namely signInAsCustomer and signInAsSeller . Both the intents take you to the same activity , but passes two different intent extras . signInAsCustomer passes extra with key "type" and value "customer" while signInAsSeler passes an extra with key "type" and value "seller" . When the second activity is started , I get the extra and depending on the extra , I set some text in a specific text view. The code for the SignInActivity is below.
SignInActivity
private String type;
private TextView tv_seller_or_customer , forgot_password , guest_login ;
private EditText email , password ;
private Button login , signUp;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Initialising Firebase Auth Object
mAuth = FirebaseAuth.getInstance();
// This is the Auth state listener . It checks if the user is aldready signed In or not
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null)
{
// User is signed in
// Goes to a new activity
Intent i = new Intent(SignInActivity.this , MainActivity.class);
startActivity(i);
}else
{
// User is signed out
// Shows sign in page to the user
signInUser();
}
}
};
}
@Override
protected void onResume() {
super.onResume();
mAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onPause() {
super.onPause();
if (mAuthStateListener != null)
{
mAuth.removeAuthStateListener(mAuthStateListener);
}
}
public void signInUser()
{
// This method initialises all the views in the signInPage
initialiseViews();
// Getting the intent extras and changing the activity depending upon the button pressed in the Intro Slider
Intent i = getIntent();
if (i != null) {
type = i.getStringExtra("type");
if (type.equals("seller")) {
tv_seller_or_customer.setText("Seller");
initialiseViews();
guest_login.setVisibility(View.GONE);
} else {
tv_seller_or_customer.setText("Customer");
initialiseViews();
}
}else
{
tv_seller_or_customer.setVisibility(View.GONE);
}
// Listener for loging in
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String emailString = email.getText().toString();
String passwordString = password.getText().toString().trim();
if (!Is_Valid_Email(emailString))
{
Toast.makeText(SignInActivity.this , "Please enter a valid Email ID", Toast.LENGTH_SHORT).show();
}else {
loginUser(emailString , passwordString);
}
}
});
}
public boolean Is_Valid_Email(String email_check) {
return !email_check.isEmpty() && isEmailValid(email_check);
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
}
public void loginUser(String emailLogin , String passwordLogin)
{
mAuth.signInWithEmailAndPassword(emailLogin , passwordLogin)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful())
{
Log.w("sign in", "Failed" , task.getException());
}else
{
Intent i = new Intent(SignInActivity.this , MainActivity.class);
startActivity(i);
}
}
});
}
public void initialiseViews()
{
forgot_password = (TextView) findViewById(R.id.forgotPassword);
guest_login = (TextView) findViewById(R.id.guest_tv);
email = (EditText) findViewById(R.id.email_edt);
password = (EditText) findViewById(R.id.pass_edt);
login = (Button) findViewById(R.id.login_button);
signUp = (Button) findViewById(R.id.sign_up_btn);
}}
The error I'm getting
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ankit_pc.pharmahouse, PID: 15750
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.ankit_pc.pharmahouse.SignInActivity.signInUser(SignInActivity.java:78)
at com.example.ankit_pc.pharmahouse.SignInActivity$1.onAuthStateChanged(SignInActivity.java:48)
at com.google.firebase.auth.FirebaseAuth$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Application terminated.
Where have I gone wrong?