【问题标题】:W/System: Ignoring header X-Firebase-Locale because its value was nullW/System:忽略标头 X-Firebase-Locale,因为它的值为 null
【发布时间】:2021-02-19 22:58:19
【问题描述】:

我对 android studio 很陌生。我正在尝试使用 Firebase 制作带有电子邮件和密码身份验证的注册页面。但是,每当我尝试点击它给出的注册按钮时:

W/System:忽略标头 X-Firebase-Locale,因为它的值为 null

谁能告诉我为什么?

这里是 SignupActivity.java 文件

package com.example.traintrack;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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;
import com.google.firebase.auth.FirebaseUser;

import java.util.regex.Pattern;

public class SignupActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    FirebaseAuth fAuth;
    Button signupBtn;       // Signup Button
    private String userType;
    private TextInputLayout textInputFullName;
    private TextInputLayout textInputEmail;
    private TextInputLayout textInputPassword;
    private boolean submitted = false, validUser = false;
    Spinner spinner;
    public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
            Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

    private String email;
    private String password;


    //Validating the signup page
    private boolean validateName(){
        String name = textInputFullName.getEditText().getText().toString().trim();

        if (name.isEmpty()){
            textInputFullName.setError("Field can't be empty");
        } else{
            textInputFullName.setError(null);
            return true;
        }

        return false;
    }
    private boolean validateEmail(){

        String email = textInputEmail.getEditText().getText().toString().trim();

        if (email.isEmpty()){
            textInputEmail.setError("Field can't be empty");
        } else if (!VALID_EMAIL_ADDRESS_REGEX.matcher(email).find()){
            textInputEmail.setError("Please enter a valid email");
        } else {
            textInputEmail.setError(null);
            return true;
        }
        return false;
    }

    private boolean validatePassword(){
        String password = textInputPassword.getEditText().getText().toString().trim();
        if (password.isEmpty()){
            textInputPassword.setError("Field can't be empty");
        } else if (password.length() < 8){
            textInputPassword.setError("Password must have at least 8 characters");
        } else {
            return true;
        }
        return false;
    }

    //public void confirm(View button){

    //}



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

        spinner = findViewById(R.id.signup_type);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.user_types, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        textInputFullName = findViewById(R.id.signup_fullname);
        textInputEmail =  findViewById(R.id.signup_email);
        textInputPassword =  findViewById(R.id.signup_password);
        signupBtn = findViewById(R.id.signup_confirm);


        //Firebase, Sign up by clicking the button
        fAuth = FirebaseAuth.getInstance();

        if (fAuth.getCurrentUser() != null)  // when the current user object is already present
          {
             startActivity(new Intent(getApplicationContext(), MainActivity.class));  //back to main page
             finish();
          }

        //register the user in firebase
        signupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password = textInputPassword.getEditText().getText().toString().trim();
                email = textInputEmail.getEditText().getText().toString().trim();

                // I have moved the validation here since this is the button for click
                submitted = true;
                if (!validateName() || !validateEmail() || !validatePassword() || !validUser){
                    Toast.makeText(SignupActivity.this, "Cannot create account", Toast.LENGTH_SHORT).show();
                    return;
                }


                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) //successfully created the user
                            {
                            Toast.makeText(SignupActivity.this, "Account created!", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent (getApplicationContext(), MainActivity.class));
                            } else {

                            Toast.makeText(SignupActivity.this, "Error !", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });




    }//OnCreated Closing


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String type = parent.getItemAtPosition(position).toString();
        if (type.equals(getResources().getStringArray(R.array.user_types)[0]) && submitted){
            TextView errorText = (TextView)spinner.getSelectedView();
            errorText.setError("");
            errorText.setTextColor(Color.RED);
            errorText.setText("Please select a user type");
        } else {
            validUser = true;
            userType = type;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}  //Ending
'''

【问题讨论】:

  • 我在物理设备上使用 wifi 时遇到了同样的问题。但是在打开了手机服务的设备上,我看不到警告。如果使用单元格数据会发生什么?
  • 您可以在此处关注这个答案stackoverflow.com/a/64657110

标签: android firebase firebase-authentication


【解决方案1】:

确保模拟器已连接到互联网。有时wifi打开但没有连接到互联网。反正我就是这样解决的。

【讨论】:

    【解决方案2】:

    您是否在 Firebase 控制台上启用了电子邮件/密码登录方法?

    【讨论】:

      【解决方案3】:

      将此行添加到应用程序标记内的 manifest.xml 中:

      android:usesCleartextTraffic="true"
      

      【讨论】:

        【解决方案4】:

        准备工作

        1. 检查您的模拟器是否有访问互联网的权限并已连接到。
        2. firebase 控制台中的电子邮件/密码登录方法已启用以获取访问权限。

        STEP 1:创建sha1密钥+调试密钥信息+添加firebase。 我猜你的 sha1 密钥可能只有 release 密钥中的正常信息?

        第 2 步:console.cloud.google.com 激活 Android 设备验证并插入由 firebase 自动生成的 sha 密钥 + 匹配的首选项(如 package name...)

        结论

        您的代码对我来说看起来不错!一切都设置正确。我想你忘记了一些琐碎的设置或正确的 sha 键。

        【讨论】:

        • 如何查看模拟器是否有权限上网?
        【解决方案5】:

        我只有在 Android Studio 上使用模拟器时才会遇到这个问题。我搜索并测试了一下,发现当我使用自己的手机启动我的应用程序时,并没有出现这个问题。就像我之前的其他人所说的那样,这可能是因为模拟器上的 wifi 连接。

        (我是Android Studio的新手,我只是分享我解决它的方法,希望对您有所帮助)

        使用它在您自己的设备上使用您的应用程序:https://developer.android.com/studio/run/device

        【讨论】:

          【解决方案6】:

          当我写一个超过 6 个字符的密码时,它对我有用。

          所以请确保您的密码长度至少为 6 个字符。

          【讨论】:

            【解决方案7】:

            我做的一个应用程序出现了这个错误,我认为错误是密码的长度,因为如果小于六位,firebase 身份验证服务将是提交错误,尝试更改长度!

            我得出这个结论是因为我从 addOnCompleteListener 中打印了 it.exception 的值,并告诉我“密码必须至少为 6 个字符”

            【讨论】:

              【解决方案8】:

              你可以写“it.exception?.printStackTrace()”,你就能知道什么是问题。 例如,我写了它,我发现了问题,我知道我没有写足够的字符-

              W/System.err:com.google.firebase.auth.FirebaseAuthWeakPasswordException:给定的密码无效。 [密码应至少为 6 个字符]

              【讨论】:

                【解决方案9】:

                确保您没有在应用中注册该电子邮件 ID。 您可以在您的 firebase 项目用户部分找到它。 如果 ID 在那里,在用户部分。 删除它,然后再次运行应用程序,现在 SignUp 应该可以工作了。 这对我有用。

                【讨论】:

                  【解决方案10】:

                  我和你有同样的错误。就我而言,在此期间,我更改了 firebase 项目。 我在 android studio 中进行了清洁,并且效果很好。 google-services.json 文件在 android studio 中生成自动生成的字符串。它必须清洗。

                  【讨论】:

                    【解决方案11】:

                    我通过使用调试密钥信息创建 sha1 密钥并添加 firebase 解决了我的问题。

                    然后我从 console.cloud.google.com 激活“Android 设备验证”并输入 firebase 自动添加的 sha 密钥并添加必要的定义(包名称等)。

                    【讨论】:

                      【解决方案12】:

                      我遇到了同样的错误,但是一旦您在 Firebase 控制台上删除了与电子邮件和密码有关的所有内容,我就会对电子邮件和密码使用 firebase Auth。这个错误似乎蔓延到我使用添加用户按钮创建了一个虚拟用户,并且错误从未消失,但我能够将数据添加到 firebase This user Button

                      【讨论】:

                        【解决方案13】:

                        我有一个具有相同电子邮件地址的帐户,因此我将其从“身份验证用户”和“数据库数据”中删除,问题已解决!

                        【讨论】:

                          【解决方案14】:

                          解决我的问题的是,firebase 将其最新的 Flutter SDK 版本升级为 null 安全性,以及所有 firebase 产品,如 firebase 消息传递等。 所以升级我所有的依赖项,特别是 firebase_auth 和 flutter 最新的稳定 SDK 版本为我做了。

                          【讨论】:

                            【解决方案15】:

                            构造一个屏幕到另一个屏幕数据发送所以请安排与一个屏幕构造函数到另一个屏幕构造函数相同*

                            另一种可能性

                            邮箱和密码误加了一点多余的空间,所以请添加这种类型的用户邮箱和密码

                            email.toString.trim();
                            password.toString.trim();
                            

                            【讨论】:

                              【解决方案16】:

                              您可能在活动的主要功能中缺少变量的实例化。 Android Studio 对细节非常挑剔。如果将其声明为变量,则需要在代码中的某处对其进行实例化。

                              【讨论】:

                                【解决方案17】:

                                它没有从 firebase 获取当前用户 uid。因此,产生了这些错误。 您需要先检查这些:

                                final User user = FirebaseAuth.instance.currentUser;
                                final uid = user.uid;
                                

                                【讨论】:

                                  【解决方案18】:

                                  重新安装模拟器并尝试使用 Play 商店应用维护模拟器

                                  【讨论】:

                                    【解决方案19】:

                                    使用以下代码行检查您使用task.getException().getMessage() 方法时遇到的错误类型。使用 toast 消息或System.out.println("Error"+);

                                    mAuth.createUserWithEmailAndPassword(email, password)
                                                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                                                    @Override
                                                    public void onComplete(@NonNull Task<AuthResult> task) {
                                                        if (task.isSuccessful()) {
                                                            // Sign in success, update UI with the signed-in user's information
                                                            FirebaseUser user = mAuth.getCurrentUser();
                                                            Toast.makeText(SignUp.this, "Success.",
                                                                    Toast.LENGTH_SHORT).show();
                                                        } else {
                                                            // If sign in fails, display a message to the user.
                                                            Toast.makeText(SignUp.this, "Error"+ task.getException().getMessage(), Toast.LENGTH_LONG).show();
                                                        }
                                                    }
                                                });
                                    

                                    我收到一条错误消息:

                                    发生内部错误。[API 密钥无效。请通过一个有效的 API 密钥]

                                    解决方案:更改项目级别 gradle 中的类路径并同步您的项目并重新安装。

                                    classpath 'com.google.gms:google-services:4.3.0'
                                    

                                    【讨论】:

                                      【解决方案20】:

                                      我遇到这个问题很多次了,经过一点耐心等待它有点工作但是控制台给我这样的通知:

                                      W/System  ( 6027): Ignoring header X-Firebase-Locale because its value was null.
                                      
                                      E/flutter ( 6027): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _SignupPageState#633fd(lifecycle state: defunct, not mounted)
                                      
                                      E/flutter ( 6027): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
                                      
                                      E/flutter ( 6027): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to 
                                      this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
                                      

                                      所以,为了解决这个错误,我在the textField 表单中添加了额外的 if mount 语句,如下所示:

                                      TextFormField(
                                          validator: (val) => val!.isEmpty ? 'Enter Email' : null,
                                          onChanged: (val) {
                                             if (mounted) {
                                                 setState(() => email = val);  //the email will trow to String that will give to firebaseAuth
                                             }
                                          },
                                          keyboardType: TextInputType.emailAddress,
                                      ),
                                      

                                      如果您在这种情况下遇到与我相同的问题,我希望它也适用于您...*干杯

                                      【讨论】:

                                        【解决方案21】:

                                        因为您正在尝试在您的 Firebase 身份验证列表中注册一个帐户,请转到 firebase console -&gt; authentication -&gt; users,然后删除该用户。尝试再次注册(只需一次,如果您与该用户重复注册,您会再次收到此错误)。

                                        【讨论】:

                                          【解决方案22】:

                                          遇到同样的问题,我在 firebase 控制台上缺少动态链接配置​​

                                          【讨论】:

                                            【解决方案23】:

                                            我遇到了同样的问题,我只是卸载应用程序并再次安装它并解决问题

                                            【讨论】:

                                              【解决方案24】:

                                              我所做的是在 Android Manifest 文件中,确保将连接到 Firebase 的 Java 类声明为低于主要活动类所在的行声明。

                                              【讨论】:

                                                【解决方案25】:

                                                您可以在将要使用的地方使用共享首选项

                                                【讨论】:

                                                • 这并不能真正回答问题。将信誉保存在共享偏好中可能是(其中一些)这些问题的解决方案,但这不是发帖人所要求的,...
                                                猜你喜欢
                                                • 2022-08-23
                                                • 2021-06-29
                                                • 2021-04-25
                                                • 2021-03-11
                                                • 1970-01-01
                                                • 2022-10-05
                                                • 2021-06-25
                                                • 2021-10-16
                                                相关资源
                                                最近更新 更多