Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin, Firebase: " Default FirebaseApp is not initialized in this process" - though initializeApp() is called?

Im starting to go crazy over that line:

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

I have no clue what is causing that. I AM calling FirebaseApp.IntApp(this) and my package name is matching ... Why is it not working?

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("2131099704");
    Firebase.FirebaseApp.InitializeApp(this);

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

    fab = FindViewById<Button>(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;
    }
}

}

like image 586
user8013509 Avatar asked Jun 08 '17 13:06

user8013509


3 Answers

Try to initialize firebase within your Application class (OnCreate method). But first download the google-services.json file from the project console, and make sure it has the "GoogleServicesJson" value as Build Action.

like image 140
alberto basoli Avatar answered Nov 03 '22 14:11

alberto basoli


I was spinning up a new app and I did not have Internet Permissions enabled. This fixed my issue

like image 27
stepheaw Avatar answered Nov 03 '22 15:11

stepheaw


One thing you don't need to do any more is to call FirebaseApp.initializeApp(this) from your MainActivity.OnCreate() method. If you're doing this, you should probably remove that call, as it's unnecessary and may cause downstream complications.

I tried deleting the bin and obj folders, I made sure that my google-services.json had the correct GoogleServicesJson build type, and that the build type was being correctly picked up in my fsproj (or csproj if you're still using C#) file. Yet the error was still happening.

What I had done wrong was that I had a typo in my package name. If you have performed all of the steps above, check the package_name field in your google-services.json file, and make sure it corresponds exactly with your project's package name. Then clean and rebuild your project.

like image 4
Rob Lyndon Avatar answered Nov 03 '22 13:11

Rob Lyndon