gpt4 book ai didi

java - 谷歌 Firebase 身份验证 Android Studio

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:57 25 4
gpt4 key购买 nike

我正在尝试在我的应用中配置新的 google firebase 身份验证。

在按钮的 OnClickListener 中运行以下命令时,我收到 cannot resolve 'addOnCOmpleteListener'

如果我将 firebase 调用从按钮 onClickListener 中移出,则会出现错误,

我不太明白为什么不能从按钮调用中访问该方法

任何帮助将不胜感激

package com.example.alex.test;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 {
private FirebaseAuth mAuth;

private FirebaseAuth.AuthStateListener mAuthListener;

Button buttonLogin;
EditText textEmail;
EditText textPassword;


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

mAuth = FirebaseAuth.getInstance();

buttonLogin = (Button) findViewById(R.id.buttonLogin);
textEmail = (EditText) findViewById(R.id.editEmail);
textPassword = (EditText) findViewById(R.id.editPassword);

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("LOG_Login", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("LOG_Login", "onAuthStateChanged:signed_out");
}
// ...
}
};




buttonLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clic

mAuth.signInWithEmailAndPassword(textEmail.getText().toString(),textPassword.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("LOG_Login", "signInWithEmail:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("LOG_Login", "signInWithEmail", task.getException());
Toast.makeText(getApplicationContext(), "Authentication failed.", Toast.LENGTH_SHORT).show();
}

// ...
}
});
}
});

}
}

我正在使用 Android Studio 2.2

我在 Firebase 中创建了一个项目

我已在 GUI 中将我的应用程序连接到此

最佳答案

If i move the firebase call out of the buttons onClickListener then the error goes,

I cant quite understand why the method is not accessible from within the button call

因为 this 是匿名类中的点击监听器。

该方法需要一个 Activity 作为 addOnCompleteListener 的第一个参数,而不是 OnClickListener。

.addOnCompleteListener(LoginActivity.this, ... 

关于java - 谷歌 Firebase 身份验证 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com