I am using firebase authentication and Realtime database. My textViewSingup was working perfectly fine to swap Activities until I added the real-time database and after that whenever I click to change the activity it crashes my app. After few tries to fix it I added a button to see if that will do the trick but it does exactly the same thing.
Here is my code.
Main Activity Code
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText editTextEmail, editTextPassword;
ProgressBar progressBar;
private Button button;
FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
editTextEmail = findViewById(R.id.editTextEmail);
editTextPassword = findViewById(R.id.editTextPassword);
progressBar = findViewById(R.id.progressBar);
findViewById(R.id.textViewSignup).setOnClickListener(this);
findViewById(R.id.buttonLogin).setOnClickListener(this);
button = (Button) findViewById(R.id.buttonRegister);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRegistration();
}
});
}
public void openRegistration(){
Intent intent = new Intent(this, SignUpActivity.class);
startActivity(intent);
}
private void userLogin(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(email.isEmpty()){
editTextEmail.setError("Please provide your Email");
editTextEmail.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextPassword.setError("Please Enter a Valid Email Address");
editTextPassword.requestFocus();
return;
}
if(password.length()<6){
editTextPassword.setError("Password is Incorrect");
editTextPassword.requestFocus();
return;
}
if(password.isEmpty()){
editTextPassword.setError("Please provide your Password");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if(task.isSuccessful()){
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.textViewSignup:
startActivity(new Intent(this, SignUpActivity.class));
break;
case R.id.buttonLogin:
userLogin();
break;
}
}
}
MainActivity.xml code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="ie.wit.ca2.MainActivity">
<EditText
android:id="#+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.374" />
<Button
android:id="#+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.63" />
<TextView
android:id="#+id/textViewSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Create an account here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.869" />
<EditText
android:id="#+id/editTextEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.233" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.477" />
<Button
android:id="#+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/buttonLogin"
app:layout_constraintVertical_bias="0.055" />
</android.support.constraint.ConstraintLayout>
SignupActivity code
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
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.FirebaseAuthUserCollisionException;
public class SignUpActivity extends AppCompatActivity implements View.OnClickListener{
ProgressBar progressBar;
EditText editTextEmail, editTextPassword;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
editTextEmail = findViewById(R.id.editTextEmail);
editTextPassword = findViewById(R.id.editTextPassword);
progressBar = findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.buttonSignUp).setOnClickListener(this);
findViewById(R.id.textViewSignup).setOnClickListener(this);
}
private void registerUser(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(email.isEmpty()){
editTextEmail.setError("Please provide your Email");
editTextEmail.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextPassword.setError("Please Enter a Valid Email Address");
editTextPassword.requestFocus();
return;
}
if(password.length()<6){
editTextPassword.setError("Password must have minimum 6 characters");
editTextPassword.requestFocus();
return;
}
if(password.isEmpty()){
editTextPassword.setError("Please provide your Password");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if(task.isSuccessful()){
Intent intent = new Intent(SignUpActivity.this, ProfileActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else{
if(task.getException() instanceof FirebaseAuthUserCollisionException){
Toast.makeText(getApplicationContext(), "This Email has already been registered", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.buttonSignUp:
registerUser();
break;
case R.id.textViewLogin:
startActivity(new Intent(this, MainActivity.class));
break;
}
}
}
This is the error i am getting when I try to change activities.
04-29 11:41:04.775 12204-12204/ie.wit.ca2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: ie.wit.ca2, PID: 12204
java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.wit.ca2/ie.wit.ca2.SignUpActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at ie.wit.ca2.SignUpActivity.onCreate(SignUpActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
So after few more hours trying to find the problem, I finally managed to notice whats wrong. In line 41 of my Signup activity, I was calling a text view that didn't exist thus making my app crash all the time.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I'm trying to make an app because of work at school but I can't solve this error (Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener(android.view.View$OnClickListener) )' on a null object reference at com.grupo1.denu1.RegisterActivity.onCreate(RegisterActivity.java:40), it occurs every time I try to open the registration activity, so I hope someone can help me
my code
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView register;
private EditText editTextEmail, editTextPassword;
private Button signIn;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register= (TextView) findViewById(R.id.register);
register.setOnClickListener (this);
signIn = (Button) findViewById(R.id.singIn);
signIn.setOnClickListener(this);
editTextEmail = (EditText) findViewById(R.id.email);
editTextPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register:
startActivity(new Intent(this, RegisterActivity.class));
break;
case R.id.singIn:
userLogin();
break;
}
}
private void userLogin() {
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (email.isEmpty()){
editTextEmail.setError("Insira o Email!");
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextEmail.setError("Coloque um email válido!");
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()){
editTextPassword.setError("Insira a senha!");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6){
editTextPassword.setError("Senha precisa ter no minimo 6 caracteres!");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(MainActivity.this, MenuBase.class));
}else{
Toast.makeText(MainActivity.this, "Falha no Login! Tente novamente",Toast.LENGTH_LONG).show();
}
}
});
}
xml:
<?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=".MainActivity">
<TextView
android:id="#+id/banner"
android:layout_width="285dp"
android:layout_height="45dp"
android:text="Denu"
android:textSize="40dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email:"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.351" />
<Button
android:id="#+id/singIn"
android:layout_width="178dp"
android:layout_height="52dp"
android:text="Entrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.572" />
<TextView
android:id="#+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Esqueceu a Senha?"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Senha:"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.465" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.793" />
<TextView
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.933" />
</androidx.constraintlayout.widget.ConstraintLayout>
RegisterActivity
package com.grupo1.denu1;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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.database.FirebaseDatabase;
import java.util.regex.Pattern;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private TextView banner, registerUser;
private EditText editTextFullName, editTextAge, editTextEmail, editTextPassword;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
banner = (TextView)findViewById(R.id.banner);
banner.setOnClickListener(this);
registerUser = (Button) findViewById(R.id.registerUser);
registerUser.setOnClickListener(this);
editTextFullName = (EditText) findViewById(R.id.fullName);
editTextAge = (EditText) findViewById(R.id.age);
editTextEmail = (EditText) findViewById(R.id.email);
editTextPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.banner:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.registerUser:
registerUser();
break;
}
}
private void registerUser() {
String email= editTextEmail.getText().toString().trim();
String password= editTextPassword.getText().toString().trim();
String fullName= editTextFullName.getText().toString().trim();
String age= editTextAge.getText().toString().trim();
if (fullName.isEmpty()){
editTextFullName.setError("Nome necessário!");
editTextFullName.requestFocus();
return;
}
if (age.isEmpty()){
editTextAge.setError("Idade necessária!");
editTextAge.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextEmail.setError("Insira um email válido!");
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()){
editTextPassword.setError("Senha necessária!");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6){
editTextPassword.setError("Senha precisa ter no minimo 6 caracteres!");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
User user = new User(fullName, age, email);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "Usuário criado com sucesso!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
//next video!
}else {
Toast.makeText(RegisterActivity.this, "Falha ao se registrar! Tente novamente!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}else {
Toast.makeText(RegisterActivity.this, "Falha ao se registrar!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
}
The solution is to move your textview to your register activity layout.
<TextView
android:id="#+id/banner"
android:layout_width="285dp"
android:layout_height="45dp"
android:text="Denu"
android:textSize="40dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
You have placed your banner textview in your main activity layout which is the root cause of your problem.
For my Android app, I have a "forget password" button. In my MainActivity.java, the code works fine to click on "Forget Password?" and transfers the user into a new java activity called ForgetPassword.java (I followed a tutorial online by Mazn). I then proceed to type in my email (that is already registered) and click "Submit" to submit my email that I forgot my password to. This only proceeds to bring me back to MainActivity.java, however, I do not believe I call it anywhere in my Password.java function.
This is my Password.java function:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
public class ForgotPassword extends AppCompatActivity {
private EditText emailEt;
private Button resetBtn;
private ProgressBar progressbar;
FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
progressbar = findViewById(R.id.progressBar4);
auth = FirebaseAuth.getInstance();
emailEt = findViewById(R.id.etEmail);
resetBtn = findViewById(R.id.btnReset);
resetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailEt.getText().toString().trim();
progressbar.setVisibility(View.VISIBLE);
auth.sendPasswordResetEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(getApplicationContext(), "Check your email to reset your password.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Try again. Something went wrong!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
activity_forgot_password.xml
<?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=".ForgotPassword">
<TextView
android:id="#+id/tvPetWatch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif-black"
android:gravity="center_horizontal"
android:text="PetWatch"
android:textAlignment="center"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The app that watches your pet!"
android:textStyle="italic"
android:textSize="18sp"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="#+id/etEmail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvPetWatch"
app:layout_constraintVertical_bias="0.0"
android:gravity="center_horizontal" />
<EditText
android:id="#+id/etEmailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="124dp"
android:ems="10"
android:hint="Enter email"
android:inputType="textEmailAddress"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDescription"
app:layout_constraintVertical_bias="0.028" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Reset Password"
android:textSize="24sp"
app:backgroundTint="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etEmailAddress"
app:layout_constraintVertical_bias="0.044" />
<ProgressBar
android:id="#+id/progressBar4"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvDescription"
app:layout_constraintVertical_bias="0.316" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my MainActivity.java function:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private EditText eEmail;
private EditText ePassword;
private Button eLogin;
private TextView register;
private ProgressBar progressBar;
private TextView forgotPassword;
boolean isValid = false;
private int counter = 3;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind them to the XML layout
// findViewById attaches the particular value to the particular XML element
eEmail = findViewById(R.id.etEmail);
ePassword = findViewById(R.id.etPassword);
eLogin = findViewById(R.id.btnLogin);
register = findViewById(R.id.tvRegister);
FirebaseApp.initializeApp(MainActivity.this);
mAuth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.progressBar);
forgotPassword = findViewById(R.id.tvForgotPassword);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.tvRegister:
startActivity(new Intent(MainActivity.this, RegisterUser.class));
break;
default:
throw new IllegalStateException("Unexpected value: " + v.getId());
}
}
});
eLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnLogin:
{
userLogin();
break;
}
}
}
});
forgotPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.tvForgotPassword:
startActivity(new Intent(MainActivity.this, ForgotPassword.class));
break;
default:
throw new IllegalStateException("Unexpected value: " + v.getId());
}
}
});
}
private void userLogin() {
String email = eEmail.getText().toString().trim();
String password = ePassword.getText().toString().trim();
if (email.isEmpty())
{
eEmail.setError("Email is required!");
eEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
eEmail.setError("Please enter a valid email address!");
eEmail.requestFocus();
return;
}
if (password.isEmpty())
{
ePassword.setError("Email is required!");
ePassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
// direct to user profile
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.isEmailVerified())
{
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
Toast.makeText(getApplicationContext(), "Login credentials was successful!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
else
{
user.sendEmailVerification();
Toast.makeText(getApplicationContext(), "Please check your email to verify your account.", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
else
{
Toast.makeText(getApplicationContext(), "Failed to login! Recheck your credentials.", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
}
activity_main.xml
<?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=".MainActivity">
<EditText
android:id="#+id/etEmail"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Enter email address"
android:inputType="textEmailAddress"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.314" />
<Button
android:id="#+id/btnLogin"
android:layout_width="380dp"
android:layout_height="70dp"
android:layout_marginTop="24dp"
android:textStyle="bold"
android:ems="10"
android:text="Login"
android:textSize="24sp"
app:backgroundTint="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPassword"
app:layout_constraintVertical_bias="0.097" />
<EditText
android:id="#+id/etPassword"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter password"
android:inputType="textPassword"
android:textSize="24sp"
android:layout_marginTop="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etEmail"
app:layout_constraintVertical_bias="0.058" />
<TextView
android:id="#+id/tvPetWatch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif-black"
android:gravity="center_horizontal"
android:text="PetWatch"
android:textAlignment="center"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The app that watches your pet!"
android:textStyle="italic"
android:textSize="18sp"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="#+id/etEmail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvPetWatch"
app:layout_constraintVertical_bias="0.0"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/tvForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password?"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.056"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnLogin"
app:layout_constraintVertical_bias="0.096" />
<TextView
android:id="#+id/tvRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New User? Register Here!"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnLogin"
app:layout_constraintVertical_bias="0.095" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your emailEt is in MainActivity, so calling findViewByID(R.id.etEmail) in ForgotPassword Activity is returning null reference.
When I clicked reset button, it seems to moving MainActivity, but in fact, the App crashes.
My local log:
2020-11-12 15:44:38.964 7425-7425/xyz.dps0340.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: xyz.dps0340.myapplication, PID: 7425
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at xyz.dps0340.myapplication.ForgotPassword$1.onClick(ForgotPassword.java:43)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
You must use etEmailAddress view in ForgotPassword activity.
Code:
Xml files are same.
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.content.Intent;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private EditText eEmail;
private EditText ePassword;
private Button eLogin;
private TextView register;
private ProgressBar progressBar;
private TextView forgotPassword;
boolean isValid = false;
private int counter = 3;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind them to the XML layout
// findViewById attaches the particular value to the particular XML element
eEmail = findViewById(R.id.etEmail);
ePassword = findViewById(R.id.etPassword);
eLogin = findViewById(R.id.btnLogin);
register = findViewById(R.id.tvRegister);
FirebaseApp.initializeApp(getApplicationContext());
mAuth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.progressBar);
forgotPassword = findViewById(R.id.tvForgotPassword);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.tvRegister:
startActivity(new Intent(MainActivity.this, RegisterUser.class));
break;
default:
throw new IllegalStateException("Unexpected value: " + v.getId());
}
}
});
eLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnLogin:
{
userLogin();
break;
}
}
}
});
forgotPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.tvForgotPassword:
Intent forgotPasswordIntent = new Intent(MainActivity.this, ForgotPassword.class);
startActivity(forgotPasswordIntent);
break;
default:
throw new IllegalStateException("Unexpected value: " + v.getId());
}
}
});
}
private void userLogin() {
String email = eEmail.getText().toString().trim();
String password = ePassword.getText().toString().trim();
if (email.isEmpty())
{
eEmail.setError("Email is required!");
eEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
eEmail.setError("Please enter a valid email address!");
eEmail.requestFocus();
return;
}
if (password.isEmpty())
{
ePassword.setError("Email is required!");
ePassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
// direct to user profile
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user.isEmailVerified())
{
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
Toast.makeText(getApplicationContext(), "Login credentials was successful!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
else
{
user.sendEmailVerification();
Toast.makeText(getApplicationContext(), "Please check your email to verify your account.", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
else
{
Toast.makeText(getApplicationContext(), "Failed to login! Recheck your credentials.", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
}
ForgotPassword.java
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
public class ForgotPassword extends AppCompatActivity {
private Button resetBtn;
private ProgressBar progressbar;
private EditText emailAddress;
FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
Intent intent = getIntent();
emailAddress = findViewById(R.id.etEmailAddress);
progressbar = findViewById(R.id.progressBar4);
auth = FirebaseAuth.getInstance();
resetBtn = findViewById(R.id.btnReset);
resetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailAddress.getText().toString();
if(email.equals("")) {
Toast.makeText(getApplicationContext(), "Your Email is wrong.", Toast.LENGTH_LONG).show();
return;
}
progressbar.setVisibility(View.VISIBLE);
auth.sendPasswordResetEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(getApplicationContext(), "Check your email to reset your password.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Try again. Something went wrong!", Toast.LENGTH_LONG).show();
}
progressbar.setVisibility(View.INVISIBLE);
}
});
}
});
}
}
When in the application, I click on a link that should bring me to the sign up page. The application crashes eventually and I receive the following error in the run log.
Error Message - Updated
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alanlyne.tbm, PID: 17423
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alanlyne.tbm/com.alanlyne.tbm.SignUpActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.alanlyne.tbm.SignUpActivity.onCreate(SignUpActivity.java:37)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Code Causing the Error
package com.alanlyne.tbm;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
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.FirebaseAuthException;
public class SignUpActivity extends AppCompatActivity {
public EditText emailId, password;
Button btnSignUp;
TextView tvSignIn;
FirebaseAuth mFirebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editText);
password = findViewById(R.id.editText2);
btnSignUp = findViewById(R.id.button2);
tvSignIn = findViewById(R.id.textView);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if (email.isEmpty()) {
emailId.setError("Please enter email");
emailId.requestFocus();
} else if (pwd.isEmpty()) {
password.setError("Please enter password");
password.requestFocus();
} else if (email.isEmpty() && pwd.isEmpty()) {
Toast.makeText(SignUpActivity.this, "Fields are empty!", Toast.LENGTH_SHORT).show();
} else if (!(email.isEmpty() && pwd.isEmpty())) {
mFirebaseAuth.createUserWithEmailAndPassword(email, pwd).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(SignUpActivity.this, "Sign Up Unsuccessful, Please Try Again", Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignUpActivity.this, HomeActivity.class));
}
}
});
} else {
Toast.makeText(SignUpActivity.this, "Error Occurred!", Toast.LENGTH_SHORT).show();
}
}
});
tvSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SignUpActivity.this, loginActivity.class);
startActivity(i);
}
});
}
}
I have also attached the XML page for the sign up activity in case that may be causing an issue.
XML for Button - Updated
<?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=".SignUpActivity">
<EditText
android:id="#+id/editText"
android:layout_width="325dp"
android:layout_height="69dp"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="231dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="569dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="326dp"
android:layout_height="66dp"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
app:layout_constraintVertical_bias="0.074" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="177dp"
android:layout_marginLeft="177dp"
android:layout_marginTop="340dp"
android:layout_marginEnd="177dp"
android:layout_marginRight="177dp"
android:layout_marginBottom="97dp"
android:text="Already have an account? Sign in here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<Button
android:id="#+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:layout_marginBottom="226dp"
android:text="Sign Up"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginLeft="176dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="178dp"
android:layout_marginRight="178dp"
android:layout_marginBottom="102dp"
android:text="Sign Up"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code of Previous Activity
package com.alanlyne.tbm;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
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 {
public EditText emailId, password;
Button btnSignIn;
TextView tvSignUp;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editText);
password = findViewById(R.id. editText2);
btnSignIn = findViewById(R.id.button2);
tvSignUp = findViewById(R.id.textView);
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
if(mFirebaseUser != null){
Toast.makeText(loginActivity.this, "You are logged in", Toast.LENGTH_SHORT).show();
Intent i = new Intent(loginActivity.this, HomeActivity.class);
startActivity(i);
}
else{
Toast.makeText(loginActivity.this, "Please login", Toast.LENGTH_SHORT).show();
}
}
};
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if (email.isEmpty()) {
emailId.setError("Please enter email");
emailId.requestFocus();
} else if (pwd.isEmpty()) {
password.setError("Please enter password");
password.requestFocus();
} else if (email.isEmpty() && pwd.isEmpty()) {
Toast.makeText(loginActivity.this, "Fields are empty!", Toast.LENGTH_SHORT).show();
} else if (!(email.isEmpty() && pwd.isEmpty())) {
mFirebaseAuth.signInWithEmailAndPassword(email, pwd).addOnCompleteListener(loginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(loginActivity.this, "Login Error, Please Try Again", Toast.LENGTH_SHORT).show();
}else{
Intent intToHome = new Intent(loginActivity.this,HomeActivity.class);
startActivity(intToHome);
}
}
});
} else {
Toast.makeText(loginActivity.this, "Error Occurred!", Toast.LENGTH_SHORT).show();
}
}
});
tvSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp = new Intent(loginActivity.this, SignUpActivity.class);
startActivity(intSignUp);
}
});
}
#Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
}
XML of Previous Activity
<?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=".loginActivity">
<EditText
android:id="#+id/editText"
android:layout_width="325dp"
android:layout_height="69dp"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="231dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="569dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="326dp"
android:layout_height="66dp"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
app:layout_constraintVertical_bias="0.074" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="177dp"
android:layout_marginLeft="177dp"
android:layout_marginTop="340dp"
android:layout_marginEnd="177dp"
android:layout_marginRight="177dp"
android:layout_marginBottom="97dp"
android:text="Not registered? Sign up here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:layout_marginBottom="226dp"
android:hint="Sign in"
android:text="Sign in"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2"
app:layout_constraintVertical_bias="0.384" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginLeft="176dp"
android:layout_marginTop="99dp"
android:layout_marginEnd="178dp"
android:layout_marginRight="178dp"
android:layout_marginBottom="102dp"
android:text="Sign In"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your reference to Button is wrong inside
SignUpActivity Activity.
Use
btnSignUp = findViewById(R.id.signUp);
Instead of
btnSignUp = findViewById(R.id.button2);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have problem with LoginNew activity. When I click "Zaloguj się" button (id LogIn) app stops working.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference error is displaying.
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zapp4, PID: 23951
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.zapp4.LoginNew.userLogin(LoginNew.java:61)
at com.example.zapp4.LoginNew.onClick(LoginNew.java:120)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Process 23951 terminated.
LoginNew.java code:
package com.example.zapp4;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
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.FirebaseAuthUserCollisionException;
public class LoginNew extends AppCompatActivity implements View.OnClickListener {
FirebaseAuth mAuth;
EditText EditTextUsername;
EditText EditTextPassword;
ProgressBar progressBar;
Button Przyciskzaloguj;
ImageView zcoinimage;
TextView zcoinnumber;
CardView LogIn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_new);
mAuth = FirebaseAuth.getInstance();
EditTextUsername = (EditText) findViewById(R.id.EditTextUsernameedit);
EditTextPassword = (EditText) findViewById(R.id.EditTextPasswordedit);
Przyciskzaloguj = (Button) findViewById(R.id.Przyciskzaloguj);
zcoinnumber = (TextView) findViewById(R.id.zcoinnumber);
zcoinimage = (ImageView) findViewById(R.id.zcoinimage);
progressBar = findViewById(R.id.progressBar);
LogIn = findViewById(R.id.LogIn);
findViewById(R.id.LogIn).setOnClickListener(this);
}
private void userLogin() {
String username = EditTextUsername.getText().toString();
String password = EditTextPassword.getText().toString().trim();
if (username.isEmpty()) {
EditTextUsername.setError("Email jest wymagany");
EditTextUsername.requestFocus();
return;
}
if (password.isEmpty()) {
EditTextPassword.setError("Hasło jest wymagane");
EditTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
EditTextPassword.setError("Minimalna długość hasła wynosi 6");
EditTextPassword.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(username).matches()) {
EditTextUsername.setError("Wprowadź poprawny email");
EditTextUsername.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(username, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
finish();
Intent intent = new Intent(LoginNew.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Przyciskzaloguj.setVisibility(View.GONE);
zcoinnumber.setVisibility(View.VISIBLE);
zcoinimage.setVisibility(View.VISIBLE);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, HomeActivity.class));
}
}
#Override
public void onClick(View view) {
if (view.getId() == R.id.LogIn) {
userLogin();
}
}
}
activity_login_new.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:background="#drawable/gradient"
tools:context=".LoginNew">
<ImageView
android:id="#+id/imageView"
android:layout_width="233dp"
android:layout_height="297dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="88dp"
android:layout_marginTop="47dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="387dp"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/znaczek_druzyny" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/EditTextUsernameedit"
android:layout_width="250dp"
android:layout_height="40dp"
android:background="#11000000"
android:hint="#string/prompt_email"
android:inputType="textPersonName"
android:textSize="16sp"
android:layout_marginBottom="35dp"
android:layout_above="#+id/EditTextPasswordedit"
android:layout_centerHorizontal="true"
android:autofillHints="" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/EditTextPasswordedit"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/EditTextUsernameedit"
android:layout_alignParentBottom="true"
android:layout_marginStart="2dp"
android:layout_marginBottom="201dp"
android:background="#11000000"
android:textSize="16sp"
android:hint="#string/prompt_password"
android:inputType="textPassword"
android:autofillHints="" />
<androidx.cardview.widget.CardView
android:id="#+id/LogIn"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="55dp"
android:layout_marginEnd="55dp"
android:layout_marginBottom="85dp"
app:cardBackgroundColor="#color/colorAccent"
app:cardCornerRadius="25dp"
app:cardElevation="10dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="13dp"
android:layout_marginBottom="13dp"
android:text="#string/action_sign_in"
android:textColor="#fff"
android:textSize="30sp" />
</androidx.cardview.widget.CardView>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="265dp"
android:layout_height="265dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="65dp"
android:layout_marginTop="230dp"
android:layout_marginEnd="65dp"
android:layout_marginBottom="230dp"
android:visibility="gone" />
</RelativeLayout>
Change
EditText EditTextUsername;
EditText EditTextPassword;
To
AppCompatEditText EditTextUsername;
AppCompatEditText EditTextPassword;
And try
First, you have to import androidx.appcompat.widget.AppCompatEditText instead of android.widget.EditText
Secondly, use AppCompatEditText instead of EditText. you should modify the following lines
FirebaseAuth mAuth;
AppCompatEditText EditTextUsername;
AppCompatEditText EditTextPassword;
...
hope it helps you.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I tried to click the signup button in the loginactivity but when click the button in the app, it will jump out and give an error like this, it's true that there are questions similar to the one I'm asking, but I really tried all the solutions you proposed but it does not work for me.
The error:
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.fivenine.shareit, PID: 17033
> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fivenine.shareit/com.fivenine.shareit.AccountActivity.SignUpActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at com.fivenine.shareit.AccountActivity.SignUpActivity.onCreate(SignUpActivity.java:44)
> at android.app.Activity.performCreate(Activity.java:6845)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
here is the SignUpActivity.java:
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.ProgressBar;
import android.widget.Toast;
import com.fivenine.shareit.MainActivity;
import com.fivenine.shareit.R;
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;
public class SignUpActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
btnResetPassword.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(new Intent(SignUpActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(), "Enter email address!",
Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(getApplicationContext(), "Enter password!",
Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6){
Toast.makeText(getApplicationContext(), "Password too short,enter minimum 6 characters!",
Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(SignUpActivity.this,"createUserWithEmail:onComplete:" + task.isSuccessful(),
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if(!task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Authenticaton faled." + task.getException(),
Toast.LENGTH_SHORT).show();
}else{
startActivity(new Intent(SignUpActivity.this,
MainActivity.class));
finish();
}
}
});
}
});
}
#Override
protected void onResume(){
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
The activity_sign_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.fivenine.shareit.AccountActivity.SignUpActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/gradient_6"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:contentDescription=""
android:src="#drawable/profile"
tools:ignore="ContentDescription" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textColor="#android:color/black"/>
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="#string/hint_password"
android:imeActionId="#integer/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#drawable/btn_corner_pink_gradient"
android:text="#string/action_sign_in_short"
android:textColor="#android:color/white"
android:textStyle="bold"/>
<!-- <Button
android:id="#+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#null"
android:text="#string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="#color/colorAccent"/> -->
<!-- Link to login screen -->
<Button
android:id="#+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#null"
android:text="#string/btn_link_to_login"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="15sp"/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>
you commented the btn_reset_password in xml. so it invokes the null object reference. Just un-comment the btn_reset_password in xml and try.
the code for 'btn_reset_password' button is commented. Just uncomment the button code and it should work.