我是android工作室的新手,我不知道如何解决这个问题。如果单击登录按钮,我的登录活动就会继续崩溃。我只是复制和替换了一些细节,从我的注册活动,它是完全正常的工作。我不知道这里有什么问题,谁来帮帮我
以下是我的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=".Login"
android:background="@drawable/bg">
<TextView
android:id="@+id/tVLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/alatsi"
android:text="LOGIN"
android:textColor="@color/sea5"
android:textSize="50sp"
android:textStyle="bold"
android:layout_marginTop="50dp"
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.097" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputEmailLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tVLogo"
android:layout_marginTop="50dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputPasswordLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/inputEmailLog"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Forgot Password"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@+id/inputPasswordLog"
app:layout_constraintTop_toBottomOf="@+id/inputPasswordLog" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:background="@drawable/bg_btn"
android:textColor="@color/white"
android:text="Login"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/forgotPassword" />
<TextView
android:id="@+id/tVNewAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Create new Account"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnLogin" />
</androidx.constraintlayout.widget.ConstraintLayout>
下面是我的java代码:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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.android.material.textfield.TextInputLayout;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
private TextInputLayout inputEmail, inputPassword;
Button log;
TextView forgotPassword, createNewAccount;
ProgressDialog mLoadingBar;
FirebaseAuth mAuth;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
inputEmail = findViewById(R.id.inputEmailLog);
inputPassword = findViewById(R.id.inputPasswordLog);
log = findViewById(R.id.btnLogin);
forgotPassword = findViewById(R.id.forgotPassword);
createNewAccount = findViewById(R.id.tVNewAccount);
createNewAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Login.this, Register.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = inputEmail.getEditText().getText().toString();
String password = inputPassword.getEditText().getText().toString();
if (email.isEmpty() || !email.contains("@gmail")){
showError(inputEmail, "Email is not valid");
} else if(password.isEmpty() || password.length()<5) {
showError(inputPassword, "Password must be more than 5 characters");
} else {
mLoadingBar.setTitle("Logging in...");
mLoadingBar.setMessage("Please wait...");
mLoadingBar.setCanceledOnTouchOutside(false);
mLoadingBar.show();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
mLoadingBar.dismiss();
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
} else {
mLoadingBar.dismiss();
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
private void showError(TextInputLayout field, String text) {
field.setError(text);
field.requestFocus();
}
}
Here's the logcat:
2022-09-29 21:43:46.331 15808-15808/com.example.asinantravelmate I/sinantravelmat: Late-enabling -Xcheck:jni
2022-09-29 21:43:46.560 15808-15808/com.example.asinantravelmate D/ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-09-29 21:43:47.502 15808-15808/com.example.asinantravelmate D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10468; state: ENABLED
2022-09-29 21:43:48.138 15808-15808/com.example.asinantravelmate V/GraphicsEnvironment: ANGLE Developer option for 'com.example.asinantravelmate' set to: 'default'
2022-09-29 21:43:48.138 15808-15808/com.example.asinantravelmate V/GraphicsEnvironment: Neither updatable production driver nor prerelease driver is supported.
2022-09-29 21:43:48.149 15808-15808/com.example.asinantravelmate D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-09-29 21:43:48.151 15808-15808/com.example.asinantravelmate D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-09-29 21:43:48.195 15808-15808/com.example.asinantravelmate I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
2022-09-29 21:43:48.310 15808-15808/com.example.asinantravelmate D/FirebaseAuth: Notifying id token listeners about user ( PFwouMYoLoVGe6uo8YQpkFjb07b2 ).
2022-09-29 21:43:48.316 15808-15808/com.example.asinantravelmate I/FirebaseInitProvider: FirebaseApp initialization successful
2022-09-29 21:43:48.429 15808-15953/com.example.asinantravelmate I/DynamiteModule: Considering local module com.google.android.gms.measurement.dynamite:83 and remote module com.google.android.gms.measurement.dynamite:79
2022-09-29 21:43:48.429 15808-15953/com.example.asinantravelmate I/DynamiteModule: Selected local version of com.google.android.gms.measurement.dynamite
2022-09-29 21:43:48.605 15808-15953/com.example.asinantravelmate V/FA: onActivityCreated
2022-09-29 21:43:48.817 15808-15808/com.example.asinantravelmate W/sinantravelmat: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
2022-09-29 21:43:48.818 15808-15808/com.example.asinantravelmate W/sinantravelmat: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
2022-09-29 21:43:48.903 15808-16064/com.example.asinantravelmate V/FA: App measurement collection enabled
2022-09-29 21:43:48.908 15808-16064/com.example.asinantravelmate V/FA: App measurement enabled for app package, google app id: com.example.asinantravelmate, 1:361014781921:android:9633899b6124c9538614a0
2022-09-29 21:43:48.913 15808-16064/com.example.asinantravelmate I/FA: App measurement initialized, version: 73000
2022-09-29 21:43:48.914 15808-16064/com.example.asinantravelmate I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2022-09-29 21:43:48.915 15808-16064/com.example.asinantravelmate I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.asinantravelmate
2022-09-29 21:43:48.915 15808-16064/com.example.asinantravelmate D/FA: Debug-level message logging enabled
2022-09-29 21:43:49.115 15808-15808/com.example.asinantravelmate W/libc: Access denied finding property "ro.vendor.perf.scroll_opt.heavy_app"
2022-09-29 21:43:49.202 15808-16064/com.example.asinantravelmate V/FA: Connecting to remote service
2022-09-29 21:43:49.245 15808-16064/com.example.asinantravelmate V/FA: Detected application was in foreground
2022-09-29 21:43:49.251 15808-16064/com.example.asinantravelmate V/FA: Session started, time: 1353029547
2022-09-29 21:43:49.275 15808-15808/com.example.asinantravelmate I/BufferQueueConsumer: [](id:3dc000000000,api:0,p:-1,c:15808) connect: controlledByApp=false
2022-09-29 21:43:49.285 15808-15808/com.example.asinantravelmate I/BLASTBufferQueue: [ViewRootImpl[MainActivity]#0] constructor()
2022-09-29 21:43:49.363 15808-16014/com.example.asinantravelmate D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-09-29 21:43:49.390 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:49.392 15808-16014/com.example.asinantravelmate E/OpenGLRenderer: Device claims wide gamut support, cannot find matching config, error = EGL_SUCCESS
2022-09-29 21:43:49.392 15808-16014/com.example.asinantravelmate W/OpenGLRenderer: Failed to initialize 101010-2 format, error = EGL_SUCCESS
2022-09-29 21:43:49.396 15808-16064/com.example.asinantravelmate I/FA: Tag Manager is not found and thus will not be used
2022-09-29 21:43:49.463 15808-16014/com.example.asinantravelmate I/BufferQueueProducer: [ViewRootImpl[MainActivity]#0(BLAST Consumer)0](id:3dc000000000,api:1,p:15808,c:15808) connect: api=1 producerControlledByApp=true
2022-09-29 21:43:49.540 15808-16155/com.example.asinantravelmate E/ion: ioctl c0044901 failed with code -1: Invalid argument
2022-09-29 21:43:49.547 15808-16014/com.example.asinantravelmate D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-09-29 21:43:49.745 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:49.767 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:49.776 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:49.776 15808-16064/com.example.asinantravelmate V/FA: Activity resumed, time: 1353029367
2022-09-29 21:43:50.056 15808-16014/com.example.asinantravelmate E/OpenGLRenderer: fbcNotifyFrameComplete error: undefined symbol: fbcNotifyFrameComplete
2022-09-29 21:43:50.056 15808-16014/com.example.asinantravelmate E/OpenGLRenderer: fbcNotifyNoRender error: undefined symbol: fbcNotifyNoRender
2022-09-29 21:43:50.091 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:50.092 15808-16064/com.example.asinantravelmate V/FA: Connection attempt already in progress
2022-09-29 21:43:50.171 15808-15808/com.example.asinantravelmate V/ImeFocusController: onWindowFocus: DecorView@56b6cff[MainActivity] softInputMode=STATE_UNSPECIFIED|ADJUST_PAN|IS_FORWARD_NAVIGATION
2022-09-29 21:43:50.171 15808-15808/com.example.asinantravelmate V/ImeFocusController: Restarting due to isRestartOnNextWindowFocus as true
2022-09-29 21:43:50.172 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=DecorView@56b6cff[MainActivity], mServedView=null
2022-09-29 21:43:50.172 15808-15808/com.example.asinantravelmate V/ImeFocusController: checkFocus: view=null next=DecorView@56b6cff[MainActivity] force=true package=<none>
2022-09-29 21:43:50.181 15808-16064/com.example.asinantravelmate D/FA: Connected to remote service
2022-09-29 21:43:50.207 15808-16064/com.example.asinantravelmate V/FA: Processing queued up service tasks: 7
2022-09-29 21:43:51.111 15808-16064/com.example.asinantravelmate V/FA: Recording user engagement, ms: 2033
2022-09-29 21:43:51.159 15808-15953/com.example.asinantravelmate V/FA: onActivityCreated
2022-09-29 21:43:51.234 15808-16064/com.example.asinantravelmate V/FA: Activity paused, time: 1353031402
2022-09-29 21:43:51.239 15808-15808/com.example.asinantravelmate D/skia: SkJpegCodec::onGetPixels +
2022-09-29 21:43:51.259 15808-15808/com.example.asinantravelmate D/skia: SkJpegCodec::onGetPixels -
2022-09-29 21:43:51.623 15808-16064/com.example.asinantravelmate V/FA: Activity resumed, time: 1353031919
2022-09-29 21:43:51.718 15808-15808/com.example.asinantravelmate I/BufferQueueConsumer: [](id:3dc000000001,api:0,p:-1,c:15808) connect: controlledByApp=false
2022-09-29 21:43:51.719 15808-15808/com.example.asinantravelmate I/BLASTBufferQueue: [ViewRootImpl[Login]#1] constructor()
2022-09-29 21:43:51.722 15808-16014/com.example.asinantravelmate I/BufferQueueProducer: [ViewRootImpl[Login]#1(BLAST Consumer)1](id:3dc000000001,api:1,p:15808,c:15808) connect: api=1 producerControlledByApp=true
2022-09-29 21:43:51.956 15808-15808/com.example.asinantravelmate V/ImeFocusController: onWindowFocus: DecorView@e3cd112[Login] softInputMode=STATE_UNSPECIFIED|ADJUST_PAN|IS_FORWARD_NAVIGATION
2022-09-29 21:43:51.957 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=DecorView@e3cd112[Login], mServedView=null
2022-09-29 21:43:51.957 15808-15808/com.example.asinantravelmate V/ImeFocusController: checkFocus: view=null next=DecorView@e3cd112[Login] force=false package=<none>
2022-09-29 21:43:52.273 15808-16014/com.example.asinantravelmate I/BufferQueueProducer: [ViewRootImpl[MainActivity]#0(BLAST Consumer)0](id:3dc000000000,api:1,p:15808,c:15808) disconnect: api 1
2022-09-29 21:43:52.284 15808-15808/com.example.asinantravelmate I/BLASTBufferQueue: [ViewRootImpl[MainActivity]#0] destructor()
2022-09-29 21:43:52.284 15808-15808/com.example.asinantravelmate I/BufferQueueConsumer: [ViewRootImpl[MainActivity]#0(BLAST Consumer)0](id:3dc000000000,api:0,p:-1,c:15808) disconnect
2022-09-29 21:43:52.308 15808-16305/com.example.asinantravelmate I/BLASTBufferQueue: releaseBufferCallbackThunk bufferId:67894843015168 framenumber:1 blastBufferQueue is dead
2022-09-29 21:43:54.437 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F.P.... 0,0-936,167}, mServedView=DecorView@e3cd112[Login]
2022-09-29 21:43:54.455 15808-15808/com.example.asinantravelmate V/ImeFocusController: checkFocus: view=DecorView@e3cd112[Login] next=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F.P..ID 0,0-936,167 aid=1073741824} force=false package=com.example.asinantravelmate
2022-09-29 21:43:54.463 15808-15808/com.example.asinantravelmate D/CompatibilityChangeReporter: Compat change id reported: 163400105; UID 10468; state: ENABLED
2022-09-29 21:43:54.521 15808-15808/com.example.asinantravelmate I/AssistStructure: Flattened final assist data: 2372 bytes, containing 1 windows, 15 views
2022-09-29 21:43:54.899 15808-15808/com.example.asinantravelmate I/AssistStructure: Flattened final assist data: 2372 bytes, containing 1 windows, 15 views
2022-09-29 21:43:55.465 15808-15808/com.example.asinantravelmate D/InsetsController: show(ime(), fromIme=true)
2022-09-29 21:43:55.521 15808-15808/com.example.asinantravelmate D/InsetsController: show(ime(), fromIme=true)
2022-09-29 21:43:56.681 15808-16064/com.example.asinantravelmate V/FA: Inactivity, disconnecting from the service
2022-09-29 21:43:59.561 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F....ID 0,0-936,167 aid=1073741824}, mServedView=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F....ID 0,0-936,167 aid=1073741824}
2022-09-29 21:43:59.612 15808-15808/com.example.asinantravelmate V/ImeFocusController: onWindowFocus: com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F....ID 0,0-936,167 aid=1073741824} softInputMode=STATE_UNSPECIFIED|ADJUST_PAN
2022-09-29 21:43:59.612 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F....ID 0,0-936,167 aid=1073741824}, mServedView=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. .F....ID 0,0-936,167 aid=1073741824}
2022-09-29 21:44:07.605 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. ........ 0,0-936,167 aid=1073741824}, mServedView=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. ........ 0,0-936,167 aid=1073741824}
2022-09-29 21:44:07.610 15808-15808/com.example.asinantravelmate D/ImeFocusController: onViewFocusChanged, view=com.google.android.material.textfield.TextInputEditText{d744ea6 VFED..CL. .F.P.... 0,0-936,167 aid=1073741825}, mServedView=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. ......ID 0,0-936,167 aid=1073741824}
2022-09-29 21:44:07.616 15808-15808/com.example.asinantravelmate V/ImeFocusController: checkFocus: view=com.google.android.material.textfield.TextInputEditText{645fcb3 VFED..CL. ......ID 0,0-936,167 aid=1073741824} next=com.google.android.material.textfield.TextInputEditText{d744ea6 VFED..CL. .F.P..ID 0,0-936,167 aid=1073741825} force=false package=com.example.asinantravelmate
2022-09-29 21:44:07.672 15808-15808/com.example.asinantravelmate I/AssistStructure: Flattened final assist data: 2396 bytes, containing 1 windows, 15 views
2022-09-29 21:44:11.502 15808-15808/com.example.asinantravelmate D/CompatibilityChangeReporter: Compat change id reported: 150939131; UID 10468; state: ENABLED
2022-09-29 21:44:12.170 15808-15808/com.example.asinantravelmate D/AndroidRuntime: Shutting down VM
--------- beginning of crash
2022-09-29 21:44:12.173 15808-15808/com.example.asinantravelmate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asinantravelmate, PID: 15808
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.setTitle(java.lang.CharSequence)' on a null object reference
at com.example.asinantravelmate.Login$2.onClick(Login.java:60)
at android.view.View.performClick(View.java:7512)
at android.view.View.performClickInternal(View.java:7489)
at android.view.View.access$3700(View.java:857)
at android.view.View$PerformClick.run(View.java:29017)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:334)
at android.app.ActivityThread.main(ActivityThread.java:8396)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
2022-09-29 21:44:12.236 15808-15808/com.example.asinantravelmate I/Process: Sending signal. PID: 15808 SIG: 9
发布于 2022-09-29 13:59:50
哈哈,现在起作用了。我忘了在java中添加mAuth和mLoadingBar的代码。谢谢亚历克斯·马莫提醒我
mAuth = FirebaseAuth.getInstance();
mLoadingBar = new ProgressDialog(this);
https://stackoverflow.com/questions/73894298
复制相似问题