Android 从Firebase恢复列表

Android 从Firebase恢复列表,android,firebase,firebase-realtime-database,android-geofence,Android,Firebase,Firebase Realtime Database,Android Geofence,如何从Firebase检索数据并存储在列表中? 需要检索此firebase列表以创建地理围栏, 我无法识别错误 这是我的班级(服务): 错误表明未创建地理围栏,但我不理解原因。这是由于onDataChange()方法的异步行为造成的,该方法在分配listageofence=geofences之前调用。因此,为了解决这个问题,您需要使用geofensesinsideonDataChange()方法。如果您想在外部使用它,只需使用该ArrayList作为参数调用一个方法,或者从这里查看我的答案 希望

如何从Firebase检索数据并存储在列表中?
需要检索此firebase列表以创建地理围栏, 我无法识别错误

这是我的班级(服务):


错误表明未创建地理围栏,但我不理解原因。

这是由于
onDataChange()
方法的异步行为造成的,该方法在分配
listageofence=geofences之前调用。因此,为了解决这个问题,您需要使用
geofenses
inside
onDataChange()
方法。如果您想在外部使用它,只需使用该
ArrayList
作为参数调用一个方法,或者从这里查看我的答案


希望能有所帮助。

我认为在传递firebase中的列表以创建
listageofence
之前使用了
listageofence

尝试删除
recurperarlocais()onCreate()
方法中选择code>,并将其放入
onConnected()
方法中。在
iniciarlocalizao()之前
添加地理围栏()


没有给@UmarZaii,按照我所做的更改,但错误仍然存在
public class ServicoLocalCliente extends Service implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener
{

    private static final String TAG = ServicoLocalCliente.class.getSimpleName();
    private GoogleApiClient googleApiClient;
    private List<Geofence> listageofence = new ArrayList<Geofence>();
    private List<Geofence> listgeo = new ArrayList<Geofence>();
    private PendingIntent geofencePendingIntent;
    private float GEOFENCE_RADIUS = 150f;
    private Geofence geofence;
    private Geofence.Builder builder;

    private Double lat ;
    private Double lng;
    private String uid ;
    private String meuUid;

    private String[] listgeoflat;
    private String[] listgeoflng;
    private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private DatabaseReference ref = database.getReference();
    private String geof;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        criarGoogleApi();
        ref = FirebaseDatabase.getInstance().getReference().child("localizacao");
        recuperarLocais();


    }
    private Geofence construirGeofences(String ui,Double lat,Double lng)
    {
        Geofence geofence = new Geofence.Builder()

                .setRequestId(ui)
                .setCircularRegion(lat,lng, GEOFENCE_RADIUS)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
                        | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
        return geofence;
    }
    private List<Geofence> recuperarLocais() {

        ref.child("localizacao").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ArrayList<Geofence> geofences = new ArrayList<Geofence>();
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    Double lat = Double.valueOf(ds.child("latitude").getValue().toString());
                    Double lng = Double.valueOf(ds.child("longitude").getValue().toString());
                    String ui = ds.getKey();
                    geofences.add(construirGeofences(ui, lat, lng));
                    listageofence = geofences;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        Log.d("test", String.valueOf(listageofence.size()));
        return listageofence;
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.i(TAG, "iniciar comando");

        if (!googleApiClient.isConnected())
            googleApiClient.connect();
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConnected(@Nullable Bundle bundle)
    {

        Log.i(TAG, "conectado " + bundle);
        Location l = null;
        try
        {
            l = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        } catch (SecurityException e)
        {
            e.printStackTrace();
        }
        if (l != null)
        {
            Log.i(TAG, "lat " + l.getLatitude());
            Log.i(TAG, "lng " + l.getLongitude());
        }

        iniciarLocalizacao();
        addGeofences();
    }

    private void addGeofences()
    {

        try
        {
            LocationServices.GeofencingApi.addGeofences(
                    googleApiClient,
                    getGeofencingRequest(),
                    getGeofencePendingIntent()
            );
        } catch (SecurityException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectionSuspended(int i)
    {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
    {

    }

    @Override
    public void onLocationChanged(Location location)
    {

        Log.i(TAG, "lat carregada " + location.getLatitude());
        Log.i(TAG, "lng carregada " + location.getLongitude());
        Toast.makeText(getApplicationContext(), "Servico de Localização Carregado", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.i(TAG, "google api desconectado");
        googleApiClient.unregisterConnectionCallbacks(this);
        LocationServices.GeofencingApi.removeGeofences(googleApiClient, getGeofencePendingIntent());
        googleApiClient.disconnect();
    }

    private void criarGoogleApi()
    {
        Log.d(TAG, "criarGoogleApi()");
        if (googleApiClient == null)
        {
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    private void iniciarLocalizacao()
    {

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(10000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        try
        {
            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);
        } catch (SecurityException e)
        {
            e.printStackTrace();
        }
    }
    private GeofencingRequest getGeofencingRequest()
    {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(listageofence);
        return builder.build();
    }
    private PendingIntent getGeofencePendingIntent()
    {
        // Reutilize o PendingIntent se já o tivermos.
       if (geofencePendingIntent != null)
       {
            return geofencePendingIntent;
       }
        Intent intent = new Intent(this, ServicoTransicIntent.class);
        // Usamos FLAG_UPDATE_CURRENT para que recebamos a mesma intenção pendente quando
        // chamando addGeofences () e removerGeofences().
        geofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
                FLAG_UPDATE_CURRENT);

        return geofencePendingIntent;
    }
07-19 13:38:32.396 9197-9197/com.example.fernandosilveira.testegeofence E/UncaughtException: java.lang.IllegalArgumentException: No geofence has been added to this request.
                                                                                                 at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
                                                                                                 at com.google.android.gms.location.GeofencingRequest$Builder.build(Unknown Source)
                                                                                                 at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.getGeofencingRequest(ServicoLocalCliente.java:231)
                                                                                                 at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.addGeofences(ServicoLocalCliente.java:157)
                                                                                                 at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.onConnected(ServicoLocalCliente.java:147)
                                                                                                 at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzaat.zzo(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzaar.zzwi(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzaar.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzaav.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzaag.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzf$zzj.zzxG(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzf$zza.zzb(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzf$zze.zzxH(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                 at android.os.Looper.loop(Looper.java:234)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-19 13:38:32.434 9197-9306/com.example.fernandosilveira.testegeofence I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:9 and remote module com.google.android.gms.tagmanager:11
07-19 13:38:32.434 9197-9306/com.example.fernandosilveira.testegeofence I/DynamiteModule: Selected remote version of com.google.android.gms.tagmanager, version >= 11
07-19 13:38:32.496 9197-9306/com.example.fernandosilveira.testegeofence W/GoogleTagManager: No container asset found in /assets/containers. Checking top level /assets directory for container assets.
07-19 13:38:32.579 9197-9306/com.example.fernandosilveira.testegeofence W/GoogleTagManager: Tag Manager's event handler WILL NOT be installed (no container loaded)
07-19 13:38:32.579 9197-9306/com.example.fernandosilveira.testegeofence I/GoogleTagManager: Tag Manager initilization took 129ms
07-19 13:38:32.711 9197-9197/com.example.fernandosilveira.testegeofence E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: com.example.fernandosilveira.testegeofence, PID: 9197
                                                                                          java.lang.IllegalArgumentException: No geofence has been added to this request.
                                                                                              at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
                                                                                              at com.google.android.gms.location.GeofencingRequest$Builder.build(Unknown Source)
                                                                                              at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.getGeofencingRequest(ServicoLocalCliente.java:231)
                                                                                              at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.addGeofences(ServicoLocalCliente.java:157)
                                                                                              at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.onConnected(ServicoLocalCliente.java:147)
                                                                                              at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
                                                                                              at com.google.android.gms.internal.zzaat.zzo(Unknown Source)
                                                                                              at com.google.android.gms.internal.zzaar.zzwi(Unknown Source)
                                                                                              at com.google.android.gms.internal.zzaar.onConnected(Unknown Source)
                                                                                              at com.google.android.gms.internal.zzaav.onConnected(Unknown Source)
                                                                                              at com.google.android.gms.internal.zzaag.onConnected(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzf$zzj.zzxG(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzf$zza.zzb(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzf$zze.zzxH(Unknown Source)
                                                                                              at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                              at android.os.Looper.loop(Looper.java:234)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-19 13:38:32.727 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Sending crashes
07-19 13:38:32.735 9197-9324/com.example.fernandosilveira.testegeofence W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
07-19 13:38:33.771 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Response code: 200
07-19 13:38:33.774 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Report sent with crash report id: d39b46e288000000
07-19 13:38:42.354 9197-9290/com.example.fernandosilveira.testegeofence W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
@Override
public void onCreate()
{
    super.onCreate();
    criarGoogleApi();
    ref = FirebaseDatabase.getInstance().getReference().child("localizacao");
    // recuperarLocais() has been removed
}

@Override
public void onConnected(@Nullable Bundle bundle)
{

    Log.i(TAG, "conectado " + bundle);
    Location l = null;
    try
    {
        l = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    } catch (SecurityException e)
    {
        e.printStackTrace();
    }
    if (l != null)
    {
        Log.i(TAG, "lat " + l.getLatitude());
        Log.i(TAG, "lng " + l.getLongitude());
    }

    recuperarLocais(); //Added here
    iniciarLocalizacao();
    addGeofences();
}