使用Firebase时出现StackoverflowError

时间:2016-09-20 11:41:23

标签: android firebase firebase-realtime-database

我目前正在尝试创建一个可以在地图上显示位置的应用,同时还会将其保存到Firebase以供日后使用。

这是我的MapsActivity:

public class MapsActivity extends AppCompatActivity
        implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    GoogleMap googleMap;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Marker marker;
    Intent intent;
    DatabaseReference myRef;
    FirebaseDatabase database;
    Longlat longlat;
    private static final String TAG ="PvCOS";

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment)  getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        intent = getIntent();
        buildGoogleApiClient();
        mGoogleApiClient.connect();
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();

    }

    //class to be sent to db
    public class Longlat {

       public double longitude;
       public double latitude;

       public Longlat() {
           // Default constructor required for calls to DataSnapshot.getValue(longlat.class)
       }

       public Longlat(Double longitude, Double latitude) {
           this.longitude = longitude;
           this.latitude = latitude;
       }

    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onMapReady(GoogleMap map) {

        googleMap = map;
        setUpMap();
    }

    public void setUpMap() {
        try {
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);
        } catch (SecurityException se){
            // TODO: 20/09/2016 handle exception
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        try {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(10);
            mLocationRequest.setFastestInterval(10);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,     mLocationRequest, this);
        } catch (SecurityException se){
            // TODO: 20/09/2016 handle exception
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        //save to server
        longlat = new     Longlat(location.getLongitude(),location.getLatitude());
        myRef.child("users").child(intent.getStringExtra("name")).setValue(longlat);
        LatLng latLng = new LatLng(location.getLatitude(),     location.getLongitude());
        marker = googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title(intent.getStringExtra("name"))
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
    protected void onStop() {
        //unregister location updates
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

        //remove previously placed Marker
        if (marker != null) {
            marker.remove();
        }
        super.onStop();
    }

}

当我运行它时,我得到一个白色屏幕并在logcat中出现此错误:

09-20 13:18:25.206 21875-21875/grp6.PvCOS E/UncaughtException: java.lang.StackOverflowError: stack size 8MB
at     java.lang.reflect.Method.invoke(Native Method)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzbw(Unknown Source)
at     com.google.android.gms.internal.zzaln.zzcd(Unknown Source)
at     com.google.android.gms.internal.zzaln$zza.zzce(Unknown Source)
at     com.google.android.gms.inter        

非常确定导致错误的Firebase部分,因为我发现另一个关于某人试图通过Firebase发送图片的线程太大了,但我发送的所有内容都是一个简单的对象。

3 个答案:

答案 0 :(得分:1)

您必须引用firebase中的JSON树节点。请检查此代码。

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("PvCOS/users");

答案 1 :(得分:0)

我刚刚误解了你是如何在Firebase中创建新孩子的。 我尝试使用.child()

添加它们
myRef = database.getReference("PvCOS/users");
myRef.child("users").child(intent.getStringExtra("name")).setValue(longlat);

但是我应该在获得参考时添加孩子:

myRef = database.getReference("PvCOS/users"+name);
myRef.setValue(longlat);

答案 2 :(得分:0)

我遇到了同样的问题:尝试使用saveValue方法保存对象时出现StackOverflow异常。

但在我的情况下问题并不是因为错误的引用而是因为使用Android的Uri对象作为我班级的一个字段。 Firebase无法保存它们,所以我不得不用String替换它们。