我的LoginActivity包含EditText,供用户输入其电子邮件和密码。在我看来,所有代码都可以。

但是,单击登录按钮后,用户不会登录。事实证明,该电子邮件无效,其中EditText电子邮件包含以下内容:

android.support.v7.widget.AppCompatEditText{b014ce3 VFED..CL. .F...... 56,646-1384,804 #7f0d0096 app:id/etEmailLogin}


请帮助我找出问题所在。

package com.example.loyalfine.myponda.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseAuth mAuth;
    private EditText etEmailLogin;
    private EditText etPasswordLogin;

    private Button bLogin;
    private Button bSwitchToRegister;
    private Button bResetPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mAuth = FirebaseAuth.getInstance();

        Toast.makeText(LoginActivity.this, (findViewById(R.id.etEmailLogin)).toString().trim(), Toast.LENGTH_LONG).show();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth         firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    Toast.makeText(LoginActivity.this, "onAuthStateChanged:signed_in:", Toast.LENGTH_LONG).show();

                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                    Toast.makeText(LoginActivity.this, "onAuthStateChanged:signed_out:", Toast.LENGTH_LONG).show();
                }
            }
        };

        etEmailLogin = (EditText) findViewById(R.id.etEmailLogin);
        etPasswordLogin = (EditText) findViewById(R.id.etPasswordLogin);
        bLogin = (Button) findViewById(R.id.bLogin);
        bLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                signIn();
            }
        });
        bSwitchToRegister = (Button) findViewById(R.id.bSwitchToRegister);
        bSwitchToRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent in = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(in);
            }
        });
        bResetPassword = (Button) findViewById(R.id.bResetPassword);
        bResetPassword.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //TODO:     com.firebase.ui.auth.ui.email.ConfirmRecoverPasswordActivity
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

    public void signIn() {
        final String passwordl = etPasswordLogin.toString().trim();
        final String emaill = etEmailLogin.toString().trim();

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        Toast.makeText(LoginActivity.this, user.getEmail()+"; "+    (findViewById(R.id.etEmailLogin)).toString().trim(), Toast.LENGTH_LONG).show();
    Log.d(TAG, emaill);

        mAuth.signInWithEmailAndPassword(emaill, passwordl);
        Intent in = new Intent(LoginActivity.this, WelcomeActivity.class);
        startActivity(in);
    };
}


我的LoginActivity布局是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.loyalfine.myponda.app.LoginActivity"
    android:orientation="vertical"
    android:gravity="center_vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your Email"
        android:id="@+id/etEmailLogin"
        android:inputType="textEmailAddress" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etPasswordLogin"
        android:ems="10"
        android:hint="Enter your password"
        android:inputType="textPassword"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/bLogin"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not registered yet? Click here for redistration!"
        android:id="@+id/bSwitchToRegister"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:background="#0000"
        android:textColor="#0a68ec"
        android:textSize="12dp"
        android:foreground="#0000" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forget password? Click here to reset your password!"
        android:id="@+id/bResetPassword"
        android:layout_gravity="center_horizontal"
        android:onClick="onClick"
        android:textColor="#ed7a41"
        android:textSize="10dp"
        android:background="#0000" />

</LinearLayout>


以下消息:

09-11 03:00:24.280 32751-32751/com.example.loyalfine.myponda.app W/ViewRootImpl[WelcomeActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=205.09277, y[0]=2066.7188, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=20468343, downTime=20463897, deviceId=0, source=0x1002 }
09-11 03:00:24.521 32751-32751/com.example.loyalfine.myponda.app D/LoginActivity: android.support.v7.widget.AppCompatEditText{b014ce3 VFED..CL. .F...... 56,646-1384,804 #7f0d0096 app:id/etEmailLogin}
09-11 03:00:24.567 32751-398/com.example.loyalfine.myponda.app W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.


这两个Toast的显示内容相同:

android.support.v7.widget.AppCompatEditText{b014ce3 VFED..CL. .F...... 56,646-1384,804 #7f0d0096 app:id/etEmailLogin}


另外,请让我知道为什么会这样:

Local module descriptor class for com.google.firebase.auth not found.


谢谢你的好心。

更新:
添加getText()onCompleteListener后,消息显示:

09-11 07:36:28.969 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[LoginActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=157.06055, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37033032, downTime=36201038, deviceId=0, source=0x1002 }
09-11 07:36:28.969 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[LoginActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=157.06055, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37033032, downTime=36201038, deviceId=0, source=0x1002 }
09-11 07:36:28.969 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[LoginActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=157.06055, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37033032, downTime=36201038, deviceId=0, source=0x1002 }
09-11 07:36:28.969 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[LoginActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=157.06055, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37033032, downTime=36201038, deviceId=0, source=0x1002 }
09-11 07:36:31.559 30405-30416/com.example.loyalfine.myponda.app I/art: Background sticky concurrent mark sweep GC freed 12136(1617KB) AllocSpace objects, 54(1220KB) LOS objects, 24% free, 9MB/12MB, paused 6.324ms total 111.816ms
09-11 07:36:31.770 30405-30405/com.example.loyalfine.myponda.app D/***LoginActivity: onAuthStateChanged:signed_in:8cG1dBsCFlNDAWiQHXAN4NSaulN2***
09-11 07:36:36.797 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[WelcomeActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=153.06152, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37040855, downTime=37035146, deviceId=0, source=0x1002 }
09-11 07:36:36.797 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[WelcomeActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=153.06152, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37040855, downTime=37035146, deviceId=0, source=0x1002 }
09-11 07:36:36.797 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[WelcomeActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=153.06152, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37040855, downTime=37035146, deviceId=0, source=0x1002 }
09-11 07:36:36.797 30405-30405/com.example.loyalfine.myponda.app W/ViewRootImpl[WelcomeActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=153.06152, y[0]=2073.6719, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=37040855, downTime=37035146, deviceId=0, source=0x1002 }
09-11 07:36:42.688 30405-30405/com.example.loyalfine.myponda.app W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
09-11 07:36:45.336 30405-30405/com.example.loyalfine.myponda.app W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
09-11 07:36:45.966 30405-30405/com.example.loyalfine.myponda.app D/LoginActivity: ***android.support.v7.widget.AppCompatEditText{2ad64b4 VFED..CL. ........ 56,646-1384,804 #7f0d0096 app:id/etEmailLogin}***
09-11 07:36:45.983 30405-30444/com.example.loyalfine.myponda.app W/DynamiteModule: Local module descriptor class for ***com.google.firebase.auth not found.***
09-11 07:36:45.983 30405-30444/com.example.loyalfine.myponda.app W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
09-11 07:36:56.234 30405-30444/com.example.loyalfine.myponda.app W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

最佳答案

首先,请确保在Firebase控制台中一切看起来都不错(有帐户等)。

现在似乎没有任何代码可以处理登录失败的情况。我刚刚在我的应用中使用Firebase实施了登录,因此这是用户按下登录按钮时使用的代码。 (注意:我刚刚注意到一个错误,该错误在于在return语句之前按钮没有再次设置为可单击,但除此之外一切都很好)

//    authenticates user, and starts main activity if successful
@OnClick(R.id.login_button)
public void login(View view) {

//        prevents the user launching the main activity multiple times if their login is valid
    loginButton.setClickable(false);

    String email = null;
    String pass = null;


    //        prevents null entries for email
    if (usernameInput.getText().length() != 0) {
        email = usernameInput.getText().toString();
    } else {
        Toast.makeText(LoginScreen.this, "Please enter your email",
                Toast.LENGTH_SHORT).show();
        return;
    }

    //        prevents null entries for pass
    if (passwordInput.getText().length() != 0) {
        pass = passwordInput.getText().toString();
    } else {
        Toast.makeText(LoginScreen.this, "Please enter your password",
                Toast.LENGTH_SHORT).show();
        return;
    }


//        signs in with the entered email and pass
    AuthManager.firebaseAuth.signInWithEmailAndPassword(email,pass)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
//                        sign in success
                    Log.v(TAG, "signIn:onComplete:" + task.isSuccessful());

//                        if sign in is successful, pass auth info to global variables, and launch intent and start the main activity
                    if (task.isSuccessful()){

                        AuthManager.user = AuthManager.firebaseAuth.getCurrentUser();
                        AuthManager.userUID = AuthManager.user.getUid();
                        AuthManager.loggedIn = true;
//                            creates the DB manager to make initial and future DB calls
                        new DBManager();

                        Toast.makeText(LoginScreen.this, "Signed in",
                                Toast.LENGTH_SHORT).show();
                        startMainActivity();
                    } else {
//                            if sign in fails
                        Log.w(TAG, "signIn", task.getException());
                        Toast.makeText(LoginScreen.this, "Incorrect username or password",
                                Toast.LENGTH_SHORT).show();
//                            makes the button clickable again
                        loginButton.setClickable(true);

                    }

                }
            });

}


我还在实现中使用了自定义AuthManager类,但是设置FirebaseAuth对象的方式非常适合它工作。

07-27 19:39