无法使用Firebase创建新用户(Xamarin.Android)

问题描述 投票:0回答:1

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

我真的不知道该怎么办,因为这是我第一次使用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());
        }
    }
}
android firebase xamarin xamarin.android visual-studio-2019
1个回答
0
投票

这不是答案,只是为了使您更轻松地进行故障排除。

第一步,您需要知道导致故障的原因。

更改您的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();
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.