问题描述
我目前正在尝试制作一个应用程序,它会在地图上显示一个位置,同时将其保存到Firebase以备后用。
这是我的MapsActivity :
公共类MapsActivity扩展AppCompatActivity
实现OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient .OnConnectionFailedListener,LocationListener {
GoogleMap google地图;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
标记标记;
意向意图;
DatabaseReference myRef;
FirebaseDatabase数据库;
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();
}
//发送到db的类
public class Longlat {
public double longitude;
公共双纬度;
$ b public Longlat(){
//调用DataSnapshot.getValue(longlat.class)所需的默认构造函数
}
public Longlat(Double经度,双纬度){
this.longitude =经度;
this.latitude =纬度;
$ b protected synchronized 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处理异常
}
}
@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处理异常
}
}
@Override
public void onConnectionSuspended(int i){
}
@Override
public void onLocationChanged(Location location){
// save到服务器
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)));
$覆盖
public void onConnectionFailed(ConnectionResult connectionResult){
protected void onStop(){
//取消注册位置更新
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
//删除先前放置的标记
if(marker!= null){
marker.remove();
}
super.onStop();
}
}
当我运行它时,一个白色的屏幕,这个错误在logcat:
09-20 13:18:25.206 21875-21875 / grp6.PvCOS E / UncaughtException:java.lang.StackOverflowError:在java.lang.reflect.Method.invoke中的堆栈大小8MB
(Native方法)$ b $ com com.google.android.gms.internal.zzaln $ zza.zzce未知源)
在com.google.android.gms.internal.zzaln.zzbw(未知源)
在com.google.android.gms.internal.zzaln.zzcd(未知源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com。 google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android。 gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
来自com.google.android.gms .internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln .zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知源)
在com.google.android.gms.internal.zzaln.zzcd(未知源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google .android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
来自com.google.android.gms .internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln .zzbw(未知来源)
在com.goog le.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android。 gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com。 google.android.gms.internal.zzaln.zzbw(未知来源)
,位于com.google.android.gms.internal.zzaln.zzcd(未知来源)
,位于com.google.android.gms。 internal.zzaln $ zza.zzce(未知来源)
a t com.google.android.gms.internal.zzaln.zzbw(未知来源)
,位于com.google.android.gms.internal.zzaln.zzcd(未知来源)
,位于com.google.android .gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal .zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw (未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
来自com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com .google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android .gms.internal.zzaln.zzcd(Unknown Sour ce)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln.zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com .google.android.gms.internal.zzaln.zzbw(未知来源)
,位于com.google.android.gms.internal.zzaln.zzcd(未知来源)
,位于com.google.android.gms .internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.internal.zzaln.zzbw(未知来源)
在com.google.android.gms.internal.zzaln .zzcd(未知来源)
在com.google.android.gms.internal.zzaln $ zza.zzce(未知来源)
在com.google.android.gms.inter
code>
很确定它的Firebase部分导致了错误,因为我发现另一个线索是关于试图通过Firebase发送图片的太大了,但我发送的只是一个简单的对象。
我刚刚误解了如何在Firebase中创建新的子项目。
我试图用.child添加它们。
$ b
myRef = database.getReference(PvCOS / users);
myRef.child(users)。child(intent.getStringExtra(name))。 setValue(longlat);
但是我应该在添加引用时添加子元素:
myRef = database.getReference(PvCOS / users+ name);
myRef.setValue(longlat);
I'm currently trying to make an app that will show you a position on a map whilst also saving it to Firebase for later use.
This is my 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();
}
}
When I run it, I get a white screen and this error in 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
Pretty sure its the Firebase part that's causing the error, as I found another thread about someone trying to send a picture over Firebase which was too big, but all I'm sending is a simple object.
I had just misunderstood how you create new children in Firebase.I tried to add them with .child
myRef = database.getReference("PvCOS/users");
myRef.child("users").child(intent.getStringExtra("name")).setValue(longlat);
But i should have just added the children when getting my reference:
myRef = database.getReference("PvCOS/users"+name);
myRef.setValue(longlat);
这篇关于使用Firebase时发生StackoverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!