【问题标题】:Android firebase connectionAndroid Firebase 连接
【发布时间】:2018-01-04 05:18:22
【问题描述】:

我试图使用电子邮件和密码连接到 Firebase 中的身份验证,但每次输入电子邮件和密码时,我都会注册失败。这是我的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button buttonRegsiter;
private EditText editTextEmail;
private EditText editTextPassword;

private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.print("In main");
    progressDialog = new ProgressDialog(this);
    firebaseAuth = FirebaseAuth.getInstance();

    buttonRegsiter = (Button) findViewById(R.id.registerUserButton);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextPassword = (EditText) findViewById(R.id.editTextpassword);

    buttonRegsiter.setOnClickListener(this);

}

private void registerUser(){
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email field is empty
        Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(password)){
        //password is empty
        Toast.makeText(this,"Please enter your password",Toast.LENGTH_LONG).show();
        return;
    }

    //if validations are ok
    //show a progressbar

    progressDialog.setMessage("Registering user...");
    progressDialog.show();

    firebaseAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
                public void onComplete(@NonNull Task<AuthResult> task){
                if(task.isSuccessful()){
                    Toast.makeText(MainActivity.this,"Registration successful",Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(MainActivity.this,"Failed to register. Please try again", Toast.LENGTH_LONG).show();
            }
              progressDialog.dismiss();
            }});
}


@Override
public void onClick(View view) {
    if(view == buttonRegsiter){
        registerUser();
    }
}
}  

你能告诉我我的代码有什么错误吗?

这是我在 logcat 上遇到的错误

01-03 23:32:49.811 1570-1620/system_process E/SoundPool:错误加载 /system/media/audio/ui/Effect_Tick.ogg

【问题讨论】:

  • 你在模拟器上测试应用吗?
  • 可能是模拟器问题,看this
  • 首先要知道是什么问题FirebaseAuthException e = (FirebaseAuthException )task.getException();Log.e("LoginActivity", "Failed Registration", e);在else情况下写这个。

标签: android firebase authentication firebase-authentication emulation


【解决方案1】:

问题是,Effect_Tick.ogg 被 System 用于触摸声音。

  1. 您可以尝试使用任何其他声音或
  2. 转到设备的声音设置并关闭“触摸声音”,然后重新启动设备/模拟器。

【讨论】:

    【解决方案2】:

    请确保您已在 Firebase 仪表板中启用 使用电子邮件/密码的登录方法

    【讨论】:

    • 是的登录方式已启用
    【解决方案3】:

    我正在使用 firebase 通过电子邮件和密码进行注册。它可能是不同的方法,但对我有用。

    public class MainActivity extends AppCompatActivity {
    
        private int SIGN_IN_REQUEST_CODE=4;
        private FirebaseAuth mAuth;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FirebaseApp.initializeApp(this);
            setContentView(R.layout.activity_main);
            mAuth = FirebaseAuth.getInstance();
    
            if(FirebaseAuth.getInstance().getCurrentUser() == null) {
                // Start sign in/sign up activity
                startActivityForResult(
                        AuthUI.getInstance()
                                .createSignInIntentBuilder()
                                .build(),
                        SIGN_IN_REQUEST_CODE
                );
            } else {
                // User is already signed in. Therefore, display
                // a welcome Toast
                Toast.makeText(this,
                        "Welcome " + FirebaseAuth.getInstance()
                                .getCurrentUser()
                                .getDisplayName(),
                        Toast.LENGTH_LONG)
                        .show();
    
                // Load chat room contents
                displayChatMessages();
            }
    
        }
    
        private void displayChatMessages() {
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if(requestCode == SIGN_IN_REQUEST_CODE) {
                if(resultCode == RESULT_OK) {
                    Toast.makeText(this,
                            "Successfully signed in. Welcome!",
                            Toast.LENGTH_LONG)
                            .show();
                    displayChatMessages();
                } else {
                    Toast.makeText(this,
                            "We couldn't sign you in. Please try again later.",
                            Toast.LENGTH_LONG)
                            .show();
    
                    // Close the app
                    finish();
                }
            }
        }
    }
    

    在(module:app)中添加这个额外的依赖,把apply插件放在(module:aap)的底部

    dependencies {
        compile 'com.google.firebase:firebase-core:9.2.0'
        compile 'com.google.firebase:firebase-auth:9.2.0'
        compile 'com.google.firebase:firebase-messaging:9.2.0'
        implementation 'com.firebaseui:firebase-ui:3.1.2'
    }
    apply plugin: 'com.google.gms.google-services'
    

    并在项目级模块中添加这个额外的依赖项。

    dependencies {
            classpath 'com.android.tools.build:gradle:3.0.1'
    
            classpath 'com.google.gms:google-services:3.1.0'
        }
    

    请在 Firebase 仪表板中使用电子邮件/密码启用登录方法

    【讨论】:

    • 我试过这样做,但事件日志显示 gradle build failed。是因为我使用的是 firebase 11.0.4 版吗?依赖项 { implementation fileTree(dir: 'libs', include: ['*.jar']) .................. 编译 'com.google.firebase:firebase-auth:11.0.4 ' 编译 'com.google.firebase:firebase-core:9.2.0' 编译 'com.google.firebase:firebase-auth:9.2.0' 实现 'com.firebaseui:firebase-ui:3.1.2' }跨度>
    • 不要使用 firebase 11.0.4 版本,不要更改上述代码中的任何内容。
    • 我试过这样做。它给出了一个错误,说使用相同的版本。所有 gms/库都应该有准确的版本。找到了两个版本 - 9.2.0 和 11.0.6。这是什么意思?
    • 这不是错误,只是警告。按原样运行,不要改变任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    相关资源
    最近更新 更多