ObjectOnNull引用上的java密码登录EditText
在LoginActivity类中,它没有显示任何错误,但每当我尝试运行应用程序并尝试通过单击登录按钮登录时,它就会失败,logcat显示ObjectOnNull引用。 我无法登录,模拟器也不会显示我所做的更新更改
import 安卓x.annotation.NonNull;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.app.Dialog;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.text.TextUtils;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import com.anisacoding.rappers.Fragments.Dashboard;
import com.google.安卓.gms.tasks.OnCompleteListener;
import com.google.安卓.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Objects;
public class LoginActivity extends AppCompatActivity {
EditText Email, Password;
TextView text_register;
Button Login;
FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
Email = findViewById(R.id.Email);
Password = findViewById(R.id.Password);
Login = findViewById(R.id.Login);
text_register = findViewById(R.id.text_register);
auth = FirebaseAuth.getInstance();
text_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view){
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
Login.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick (View view){
final Dialog pd = new Dialog( LoginActivity.this);
pd.setTitle("Please wait.....");
pd.show();
String str_Email = Email.getText().toString();
@NonNull
String str_Password = Password.getText().toString();
if (TextUtils.isEmpty(str_Email) || TextUtils.isEmpty(str_Password)) {
Toast.makeText(LoginActivity.this, "All fields are required, all texts", Toast.LENGTH_SHORT).show();
} else {
auth.signInWithEmailAndPassword(str_Email, str_Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(Objects.requireNonNull(auth.getCurrentUser()).getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
pd.dismiss();
Intent intent = new Intent(LoginActivity.this, Dashboard.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
pd.dismiss();
}
});
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "Authentication failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
} );
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center"
安卓:orientation="vertical"
安卓:padding="15dp"
tools:context=".LoginActivity">
<EditText
安卓:id="@+id/Email"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginEnd="9dp"
安卓:autofillHints=""
安卓:background="@drawable/edittext_background"
安卓:hint="@string/email_hin"
安卓:inputType="textEmailAddress"
安卓:padding="10dp"
安卓:text="@string/email_text"
app:layout_constraintBottom_toTopOf="@+id/Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.641" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="10dp"
安卓:layout_marginEnd="9dp"
安卓:autofillHints=""
安卓:background="@drawable/edittext_background"
安卓:hint="@string/password_hint"
安卓:inputType="textPassword"
安卓:padding="10dp"
安卓:text="@string/password_text"
app:layout_constraintBottom_toTopOf="@+id/Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.795" />
<Button
安卓:id="@+id/Login"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="252dp"
安卓:background="@drawable/button_background"
安卓:text="@string/log_in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent" />
<TextView
安卓:id="@+id/text_register"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="106dp"
安卓:textColor="@color/white"
安卓:layout_marginTop="37dp"
安卓:layout_marginEnd="95dp"
安卓:layout_marginBottom="196dp"
安卓:text="@string/register_question"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</安卓x.constraintlayout.widget.ConstraintLayout>
Logcat:
java.lang.NullPointerException: Attempt to invoke virtual method '安卓.text.Editable 安卓.widget.EditText.getText()' on a null object reference
at com.anisacoding.rappers.LoginActivity$2.onClick(LoginActivity.java:68)
at 安卓.view.View.performClick(View.java:7448)
at 安卓.view.View.performClickInternal(View.java:7425)
at 安卓.view.View.access$3600(View.java:810)
at 安卓.view.View$PerformClick.run(View.java:28305)
at 安卓.os.Handler.handleCallback(Handler.java:938)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:223)
at 安卓.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-09-05 17:02:13.661 3279-3399/com.google.安卓.googlequicksearchbox E/MDD: DownloadProgressMonitor: Can't find file group for uri: 安卓://com.google.安卓.googlequicksearchbox/files/sharedminusonemodule/shared/SharedMinusOneData.pb.tmp
2020-09-05 17:02:14.915 1066-2168/com.google.安卓.apps.nexuslauncher E/ActivityThread: Failed to find provider info for com.google.安卓.apps.wellbeing.api
2020-09-05 17:02:41.700 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:02:41.700 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:02:47.997 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:02:47.997 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:03:06.393 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:03:07.265 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:03:07.452 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:03:33.037 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:03:33.244 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:03:41.773 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:03:41.779 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:03:48.017 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:03:48.017 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:04:41.814 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:04:41.815 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:04:48.047 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:04:48.047 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:04:57.706 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:04:57.863 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:04:57.934 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:05:10.578 508-572/system_process E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)
2020-09-05 17:05:10.607 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:05:10.628 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:05:12.791 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:05:12.809 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:05:41.850 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:05:41.850 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:05:48.098 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:05:48.099 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:06:41.905 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:06:41.905 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:06:47.976 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:06:48.025 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:06:48.079 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:06:48.112 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:06:48.112 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:07:03.137 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:07:03.159 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:07:14.323 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:07:14.349 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:07:30.518 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:07:30.541 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:07:41.955 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:07:41.955 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:07:48.167 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:07:48.167 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:08:14.592 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:08:14.679 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:08:31.447 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:08:31.476 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:08:34.801 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:08:34.839 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:08:38.191 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:08:42.005 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:08:42.005 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:08:48.236 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:08:48.257 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:08:57.727 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:08:58.606 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:09:42.114 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:09:42.115 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:09:48.349 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:09:48.350 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:09:54.944 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:09:55.683 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:10:12.297 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:10:12.367 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:10:28.393 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:10:28.486 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:10:28.541 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:10:42.225 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:10:42.225 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:10:43.801 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:10:43.825 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:10:48.396 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:10:48.397 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:11:42.154 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:11:42.154 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:11:48.446 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-09-05 17:11:48.446 362-362/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-09-05 17:12:20.847 8137-10175/com.google.安卓.gms E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2020-09-05 17:12:27.764 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:12:27.784 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:12:35.931 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-09-05 17:12:35.996 170-176/? E/安卓.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-09-05 17:12:42.204 399-399/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-09-05 17:12:42.204 399-399/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-09-05 17:12:48.498 362-362/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
如何解决这个问题
共 (0) 个答案