得到错误" Java.Lang.IllegalStateException"使用FirebaseDatabase应用程序

时间:2018-01-06 03:29:13

标签: android firebase xamarin visual-studio-2017

我正在线上关于如何使用Xamarin创建一个Android聊天应用程序的教程,该应用程序是在Visual Studio 2015中实现的,而我使用的是Visual Studio 2017.我还尝试使用InitializeApp查看是否可以修复它,但它确实不行。可能是什么问题呢?我实施了一些更改,但我有一个问题。我收到错误" Java.Lang.IllegalStateException:"在线FirebaseDatabase.Instance.GetReference("chats").AddValueEventListener(this);

这就是我实现OnCreate方法的方法:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
        FirebaseApp.InitializeApp(this);

        firebase = new FirebaseClient(FirebaseURL);
        //firebase = new FirebaseClient(GetString(Resource.String.firebase_url)); //not working
        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;
    }

0 个答案:

没有答案