I am trying to an app for login app for emplyees, I have made three activities MainActivity, Register and Login. I have only designed the register activity and an onclick listener in login, and if the user is not logged in, the user is directed to Login, and an onclick listener to Register.Java, There is no error but the app keeps crashing and whenever i am opening the app in the emulator, the app does not open and it shows the this app keeps stopping.
MainActivity.java
package com.example.employeedatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
FirebaseUser currentUser;
public void updateUI(FirebaseUser account){
if(account != null){
Toast.makeText(this,"U Signed In successfully",Toast.LENGTH_LONG).show();
startActivity(new Intent(this,this.getClass()));
}else {
Toast.makeText(this,"U Didnt signed in", Toast.LENGTH_LONG).show();
startActivity(new Intent(this,Login.class));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
}
Login.java
package com.example.employeedatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Login extends AppCompatActivity {
TextView Nuser = findViewById(R.id.new_user);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Nuser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
}
}
And at last there is Register.java
package com.example.employeedatabase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Register extends AppCompatActivity {
private FirebaseAuth mAuth;
private static final String TAG = "Register";
public void updateUI(FirebaseUser account){
if(account != null){
Toast.makeText(this,"U Signed In successfully",Toast.LENGTH_LONG).show();
startActivity(new Intent(this,this.getClass()));
}else {
Toast.makeText(this,"U Didnt signed in", Toast.LENGTH_LONG).show();
startActivity(new Intent(this,Login.class));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
String email = findViewById(R.id.email).toString();
String password = findViewById(R.id.password).toString();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "createUserWithEmail : Success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
}
else {
Log.w(TAG, "createUserWithEmail : Failure", task.getException());
Toast.makeText(Register.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
}
I want that if the user is not logged in it should open login.java, and if user click this text 'New User?, Sign In!' I want the user to go to the Register.java
My Activity_register.xml is as follows.-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Register">
<TextView
android:id="@+id/textView2"
android:layout_width="228dp"
android:layout_height="52dp"
android:text="Register"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.743"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048" />
<EditText
android:id="@+id/email"
android:layout_width="263dp"
android:layout_height="39dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.479"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.187" />
<EditText
android:id="@+id/password"
android:layout_width="257dp"
android:layout_height="40dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email"
app:layout_constraintVertical_bias="0.14" />
<Button
android:id="@+id/Rdone"
android:layout_width="143dp"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="Register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.24000001" />
</androidx.constraintlayout.widget.ConstraintLayout>