user8013509
user8013509

Reputation: 151

Xamarin, Android, Firebase: "Defualt FirebaseApp is not initialized error"... ?

I am working on a chat app and found a few examples on how to implement Firebase into Xamarin. I think I've done everything just like in the tutorials, yet I am getting this error:

Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process XamarinChatApp.XamarinChatApp. Make sure to call FirebaseApp.initializeApp(Context) first.

So, all I changed then is to add this line of code before the error happens:

    Firebase.FirebaseApp.InitializeApp(this);

Yet, this did not change anything.

This is the complete main activity:

    public class MainActivity : AppCompatActivity,IValueEventListener
    {
        private FirebaseClient firebase;
        private List<MessageContent> lstMessage = new List<MessageContent>();
        private ListView lstChat;
        private EditText edtChat;
        private FloatingActionButton fab;

        public int MyResultCode = 1;


        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
        }



        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            firebase = new FirebaseClient(GetString(Resource.String.firebase_database_url));
            Firebase.FirebaseApp.InitializeApp(this);
            FirebaseDatabase.Instance.GetReference("chats").AddValueEventListener(this);

            fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            edtChat = FindViewById<EditText>(Resource.Id.input);
            lstChat = FindViewById<ListView>(Resource.Id.list_of_messages);


            fab.Click += delegate
            {

                PostMessage();
            };



            if (FirebaseAuth.Instance.CurrentUser == null)
                StartActivityForResult(new Android.Content.Intent(this, typeof(SignIn)), MyResultCode);
            else
            {
                Toast.MakeText(this, "Welcome " + FirebaseAuth.Instance.CurrentUser.Email, ToastLength.Short).Show();
                DisplayChatMessage();
            }
        }

        private async void PostMessage()
        {
            var items = await firebase.Child("chats").PostAsync(new MessageContent(FirebaseAuth.Instance.CurrentUser.Email, edtChat.Text));
            edtChat.Text = "";
        }

        public void OnCancelled(DatabaseError error)
        {

        }

        public void OnDataChange(DataSnapshot snapshot)
        {
            DisplayChatMessage();
        }

        private async void DisplayChatMessage()
        {
            lstMessage.Clear();
            var items = await firebase.Child("chats")
                .OnceAsync<MessageContent>();

            foreach (var item in items)
                lstMessage.Add(item.Object);
            ListViewAdapter adapter = new ListViewAdapter(this, lstMessage);
            lstChat.Adapter = adapter;
            }
        }
    }

The Problem happens in OnCreate, right at:

FirebaseDatabase.Instance.GetReference("chats").AddValueEventListener(this);

I have found another solution to this on this forum, but all they did was add the above line and it worked. For me, it unfortunately doesn't.

Any help would be awesome :) THANKS

Upvotes: 1

Views: 1422

Answers (1)

user8013509
user8013509

Reputation: 151

Okay, the mistake was simple: I had a different package name in the firebase console than in my app.

Upvotes: 1

Related Questions