【问题标题】:Unable to Create a New User using Firebase (Xamarin.Android)无法使用 Firebase (Xamarin.Android) 创建新用户
【发布时间】:2020-04-29 03:23:43
【问题描述】:

每当我尝试通过应用程序注册用户时,即使满足所有要求,它也总是失败。我包含在清单中,但这并没有解决任何问题。还启用了电子邮件/密码登录方法。

我真的不知道该怎么做,因为这是我第一次使用 xamarin,而且在 xamarin 表单与 xamarin android 方面似乎有更多帮助。

    public class registerActivity : AppCompatActivity
    {
        TextInputLayout fullNameText;
        TextInputLayout phoneText;
        TextInputLayout emailtext;
        TextInputLayout passwordText;
        Button registerButton;

        FirebaseAuth firebaseAuth;
        FirebaseDatabase userCredentials;

        taskCompletetionListener taskCompletetionListener = new taskCompletetionListener();
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.registerPage);

            Typeface tf = Typeface.CreateFromAsset(Assets, "Tw_Cen_MT.TTF");

            var signUpTxt = FindViewById<TextView>(Resource.Id.signUpTxt);
            signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);

            var signUpLink = FindViewById<TextView>(Resource.Id.signInLink);
            signUpLink.SetTypeface(tf, TypefaceStyle.Bold);

            var alreadyMemberTxt = FindViewById<TextView>(Resource.Id.haveAccTxt);
            signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);

            Button signUpBtn = (Button)FindViewById(Resource.Id.registerBtn);
            signUpBtn.SetTypeface(tf, TypefaceStyle.Bold);

            InitializedFirebase();
            firebaseAuth = FirebaseAuth.Instance;
            ConnectControl();
        }

        void InitializedFirebase()
        {
            var app = FirebaseApp.InitializeApp(this);

            if (app == null)
            {
                var options = new FirebaseOptions.Builder()
                    .SetApplicationId("semoto-4b53b")
                    .SetApiKey("AIzaSyBiGjJQwDewsyWxviBjIimw7pQBHkbHkiA")
                    .SetDatabaseUrl("https://semoto-4b53b.firebaseio.com")
                    .SetStorageBucket("semoto-4b53b.appspot.com")
                    .Build();

                app = FirebaseApp.InitializeApp(this, options);
                userCredentials = FirebaseDatabase.GetInstance(app);
            }

            else
            {
                userCredentials = FirebaseDatabase.GetInstance(app);
            }
        }

        void ConnectControl()
        {
            fullNameText = (TextInputLayout)FindViewById(Resource.Id.nameField);
            emailtext = (TextInputLayout)FindViewById(Resource.Id.emailField);
            phoneText = (TextInputLayout)FindViewById(Resource.Id.phoneNumField);
            passwordText = (TextInputLayout)FindViewById(Resource.Id.passwordField);
            registerButton = (Button)FindViewById(Resource.Id.registerBtn);

            registerButton.Click += RegisterButton_Click;
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            string fullName, phoneNum, email, password;
            fullName = fullNameText.EditText.Text;
            phoneNum = phoneText.EditText.Text;
            email = emailtext.EditText.Text;
            password = passwordText.EditText.Text;

            if(fullName.Length <= 3)
            {
                fullNameText.ErrorEnabled = true;
                fullNameText.Error = "Please enter a valid name";
                return;
            }
            else if (!email.Contains("@"))
            {
                emailtext.ErrorEnabled = true;
                emailtext.Error = "Please enter a valid email";
                return;
            }
            else if(phoneNum.Length < 9)
            {
                phoneText.ErrorEnabled = true;
                phoneText.Error = "Please enter a valid phone number";
                return;
            }
            else if (password.Length < 8)
            {
                passwordText.ErrorEnabled = true;
                passwordText.Error = "Password should be at least 8 characters long";
                return;
            }

            registerUser(fullName, phoneNum, email, password);
        }

        void registerUser(string name, string phone, string email, string password)
        {
            taskCompletetionListener.success += TaskCompletetionListener_success;
            taskCompletetionListener.failure += TaskCompletetionListener_failure;
            firebaseAuth.CreateUserWithEmailAndPassword(email, password)
                .AddOnSuccessListener(this, taskCompletetionListener)
                .AddOnFailureListener(this, taskCompletetionListener);

        }

        private void TaskCompletetionListener_failure(object sender, EventArgs e)
        {
            Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
            alert.SetTitle("Registration Failed!");
            alert.SetMessage("Ensure that you meet all of the requirements. ");
            alert.SetNeutralButton("OK", delegate
            {
                alert.Dispose();
            });
            alert.Show();
        }
        //reishinishite kudasai
        private void TaskCompletetionListener_success(object sender, EventArgs e)
        {
            Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
            alert.SetTitle("Registration Successful");
            alert.SetMessage("Welcome!");
            alert.SetNeutralButton("OK", delegate
            {
                alert.Dispose();
            });
            alert.Show();
        }
    }
}

我为任务完成监听器做了一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Gms.Tasks;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Semoto.eventListeners
{
    public class taskCompletetionListener : Java.Lang.Object, IOnSuccessListener, IOnFailureListener
    {
        public event EventHandler success;
        public event EventHandler failure;
        public void OnFailure(Java.Lang.Exception e)
        {
            failure?.Invoke(this, new EventArgs());
        }

        public void OnSuccess(Java.Lang.Object result)
        {
            success?.Invoke(this, new EventArgs());
        }
    }
}

【问题讨论】:

  • 失败怎么办?它是抛出异常还是错误?是否调用了 CreateUserWithEmailAndPassword?它的完成处理程序被调用了吗?你在检查返回值吗?
  • 出现注册失败提示。 CreateUserWithEmailAndPassword 在注册用户函数中被调用。我应该检查哪个返回值? @杰森
  • 我对 Firebase 一无所知,但我认为 EventArgs 将包含有关失败原因的信息。

标签: android firebase xamarin xamarin.android visual-studio-2019


【解决方案1】:

这不是答案,只是为了方便您排除故障。

第一步,您需要知道导致失败的原因。

更改您的 OnFailure 监听器,例如:

 public void OnFailure(Java.Lang.Exception e)
  {
     failure?.Invoke(e, new EventArgs());
  }

然后在您的活动中,打印错误消息,这里使用您的对话框显示:

 private void TaskCompletetionListener_failure(object sender, EventArgs e)
    {
        Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
        alert.SetTitle("Registration Failed!");
        alert.SetMessage(((Java.Lang.Exception)sender).Message);
        alert.SetNeutralButton("OK", delegate
        {
            alert.Dispose();
        });
        alert.Show();
    }

然后您可以显示您的错误消息,我们可以帮助您更快地找到解决方案。(Ps:我使用您的代码用我的帐户进行测试,效果很好)。

【讨论】:

  • 非常感谢!我想检查错误消息,但我真的不知道该怎么做。原来我的 API 密钥是错误的。
  • @Ashley 是的,当您知道错误信息时,您可以快速找到错误。
猜你喜欢
  • 2018-08-14
  • 2017-12-26
  • 2020-03-20
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多