Я делаю экран входа для своего приложения, в итоге у меня два активити, одна просто для входа, другая для регистрации, проблема в том что при нажатии на кнопки приложение сразу же закрывается и все, в чем может быть проблема?
Вот код: Экран входа:
package com.example.afec;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivityEntrance extends AppCompatActivity implements View.OnClickListener {
// Обьявление об использовании следующих обьектов:
Button button_create;
TextView textForgot;
Button button_enter;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
EditText editEntranceTextTextEmailAddress;
EditText editEntranceTextTextPassword3;
RelativeLayout root;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_entrance);
button_create = findViewById(R.id.button_create);
button_create.setOnClickListener(this);
textForgot = findViewById(R.id.textForgot);
textForgot.setOnClickListener(this);
button_enter = findViewById(R.id.button_enter);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("Users");
}
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(editEntranceTextTextEmailAddress.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (editEntranceTextTextPassword3.getText().toString().length() < 5) {
Snackbar.make(root, "Короткий пороль, введите болешь 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
auth.signInWithEmailAndPassword(editEntranceTextTextEmailAddress.getText().toString(), editEntranceTextTextPassword3.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivityEntrance.this, MainActivityForgotPassword.class));
finish();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(root, "Ошибка авторизации. " + e.getMessage(), Snackbar.LENGTH_SHORT).show();
}
});
}
}
Экран регистрации
package com.example.afec;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.example.afec.Models.User;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Objects;
public class MainActivityCreateAccount extends AppCompatActivity implements View.OnClickListener {
Button button_create_account;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
EditText editCreateAccountTextTextEmailAddress;
EditText editCreateAccountTextTextPassword3;
RelativeLayout root;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_create_account);
button_create_account = findViewById(R.id.button_create_account);
button_create_account.setOnClickListener(this);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("Users");
editCreateAccountTextTextEmailAddress = findViewById(R.id.editEntranceTextTextEmailAddress);
editCreateAccountTextTextPassword3 = findViewById(R.id.editEntranceTextTextPassword3);
root = findViewById(R.id.root_element);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(editCreateAccountTextTextEmailAddress.getText().toString())) {
Snackbar.make(root, "Введите вашу почту", Snackbar.LENGTH_SHORT).show();
return;
}
if (editCreateAccountTextTextPassword3.getText().toString().length() < 5) {
Snackbar.make(root, "Короткий пороль, введите болешь 5 символов", Snackbar.LENGTH_SHORT).show();
return;
}
// Регистрация
auth.createUserWithEmailAndPassword(editCreateAccountTextTextEmailAddress.getText().toString(), editCreateAccountTextTextPassword3.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
User user = new User();
user.setEditCreateAccountTextTextEmailAddress(editCreateAccountTextTextEmailAddress.getText().toString());
user.setEditCreateAccountTextTextPassword3(editCreateAccountTextTextPassword3.getText().toString());
users.child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Snackbar.make(root, "Успешная регистрация", Snackbar.LENGTH_SHORT).show();
}
});
}
});
}
}
XML регистрации
<?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"
tools:context=".MainActivityCreateAccount"
android:background="@color/grey">
<ImageView
android:id="@+id/Logo"
android:layout_width="match_parent"
android:layout_height="236dp"
android:layout_marginStart="88dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="88dp"
android:foregroundGravity="center"
android:contentDescription="@string/icon"
app:layout_constraintBottom_toTopOf="@id/editCreateAccountTextTextEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_logo_foreground"
tools:ignore="ImageContrastCheck,ImageContrastCheck" />
<!-- Это текс отвечающий за информирование для создания аккаунта -->
<TextView
android:id="@+id/textCreateAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Logo"
android:layout_marginHorizontal="139dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:text="@string/createText"
android:textColor="@color/black"
android:textSize="15sp"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editCreateAccountTextTextEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foregroundGravity="center"
android:layout_below="@id/textCreateAccount"
android:layout_marginStart="88dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="88dp"
android:background="@drawable/edit_text_style"
android:ems="10"
android:hint="@string/Username_hint"
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="@android:color/black"
app:layout_constraintHorizontal_bias="0.485"
tools:ignore="MissingConstraints,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<EditText
android:id="@+id/editCreateAccountTextTextPassword3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editCreateAccountTextTextEmailAddress"
android:layout_marginStart="88dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="4dp"
android:background="@drawable/edit_text_style"
android:ems="10"
android:hint="@string/Password_hint"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="@android:color/black"
app:layout_constraintBottom_toTopOf="@+id/button_create_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<Button
android:id="@+id/button_create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editCreateAccountTextTextPassword3"
android:layout_marginStart="88dp"
android:layout_marginEnd="88dp"
android:backgroundTint="@color/green"
android:text="@string/create_on_account_button"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints,TextContrastCheck,TouchTargetSizeCheck" />
</RelativeLayout>
XML входа
<?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"
tools:context=".MainActivityEntrance"
android:background="@color/grey"
android:id="@+id/root_element">
<!-- Снизу это Иконка -->
<ImageView
android:id="@+id/Logo"
android:layout_width="match_parent"
android:layout_height="236dp"
android:layout_marginStart="88dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="88dp"
android:contentDescription="@string/icon"
app:layout_constraintBottom_toTopOf="@+id/editEntranceTextTextEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_logo_foreground"
tools:ignore="ImageContrastCheck,ImageContrastCheck" />
<!-- Снизу это поле для ввода электронной почты для входа -->
<EditText
android:id="@+id/editEntranceTextTextEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Logo"
android:layout_marginStart="88dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="88dp"
android:background="@drawable/edit_text_style"
android:ems="10"
android:hint="@string/Username_hint"
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="@android:color/black"
app:layout_constraintBottom_toTopOf="@+id/editEntranceTextTextPassword3"
app:layout_constraintEnd_toEndOf="@+id/Logo"
app:layout_constraintHorizontal_bias="0.485"
app:layout_constraintStart_toStartOf="@+id/Logo"
app:layout_constraintTop_toBottomOf="@+id/Logo"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<!-- Снизу это поле для ввода пороля для входа -->
<EditText
android:id="@+id/editEntranceTextTextPassword3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editEntranceTextTextEmailAddress"
android:layout_marginStart="88dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="4dp"
android:background="@drawable/edit_text_style"
android:ems="10"
android:hint="@string/Password_hint"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="@android:color/black"
app:layout_constraintBottom_toTopOf="@+id/button_enter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editEntranceTextTextEmailAddress"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<!-- Это кнопка отвечающая за вход в приложения -->
<Button
android:id="@+id/button_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editEntranceTextTextPassword3"
android:layout_marginStart="88dp"
android:layout_marginEnd="88dp"
android:backgroundTint="@color/green"
android:text="@string/login_hint"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@id/textForgot"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.571"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editEntranceTextTextPassword3"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
<!-- Это кнопка отвечающая за переход на страницу для сброса пороля -->
<TextView
android:id="@+id/textForgot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editEntranceTextTextPassword3"
android:layout_marginHorizontal="150dp"
android:layout_marginTop="38sp"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:minHeight="48dp"
android:text="@string/Forgot_hint"
android:textColor="@color/black"
android:textSize="14sp" />
<!-- Это текс отвечающий за информирование кнопки о Создании аккаунта -->
<TextView
android:id="@+id/textCreate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editEntranceTextTextEmailAddress"
android:layout_marginHorizontal="165dp"
android:layout_marginTop="229dp"
android:text="@string/no_account_text"
android:textColor="@color/black"
android:textSize="12sp" />
<!-- Это кнопка для создание аккаунта, в случае если его нету -->
<Button
android:id="@+id/button_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_enter"
android:layout_marginStart="105dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="106dp"
android:layout_marginBottom="8dp"
android:backgroundTint="@color/white"
android:text="@string/create_on_account_button"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_enter"
tools:ignore="TouchTargetSizeCheck" />
</RelativeLayout>
Лог ошибки при нажатии на кнопку
022-12-10 22:01:34.284 20990-20990/com.example.afec E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.afec, PID: 20990
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.afec.MainActivityEntrance.onClick(MainActivityEntrance.java:57)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)