0

I'm using firesharp, I try to retrieve data in my mobile app but when I try it the app crashes on start before showing text. Is there any way I did something wrong? or just the firesharp isnt supposed to be for Xamarin. I did this already with a program that has insert and retrieve but here I want only to retrieve and then insert

MainActivity:

 using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using FireSharp.Config;
    using FireSharp.Interfaces;
    using FireSharp.Response;
    using System.Threading;
    using System;

    namespace Quiz__
    {
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : AppCompatActivity
        {

            IFirebaseConfig config = new FirebaseConfig
            {
                AuthSecret = "Im hiding it",
                BasePath = "Im hiding it"
            };
            IFirebaseClient client;
            protected override async void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                client = new FireSharp.FirebaseClient(config);
                TextView txtQuestion = FindViewById<TextView>(Resource.Id.textQuestion);
                var g = 2;
//g variable is just for the test
                FirebaseResponse response = await client.GetAsync("question/"+g);
                Data obj = response.ResultAs<Data>();
                txtQuestion.Text = obj.question;

            }
        }
    }

AXML:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">

        <TextView
            android:id="@+id/textQuestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="35sp"
        />

    </RelativeLayout>

Data class:

namespace Quiz__
{
    internal class Data
    {
        public string Id { get; set; }
        public string question { get; set; }
        public string answer { get; set; }
        public string hint { get; set; }
    }
}

I did this already with a program that has insert and retrieve but here I want only to retrieve and then insert

ramya br
  • 225
  • 2
  • 17

1 Answers1

0

You can move client.GetAsync("question/"+g) in other method and don't use async for OnCreate method, and try your project again.

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        client = new FireSharp.FirebaseClient(config);
        txtQuestion = FindViewById<TextView>(Resource.Id.textQuestion);

        //g variable is just for the test
        test();
    }

    public async void test()
    {
        var g = "test1";
        FirebaseResponse response = await client.GetAsync("question/" + g);
        Data obj = response.ResultAs<Data>();
        txtQuestion.Text = obj.question;
    }
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16