代码:
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
public String Latvalue;
public String Longvalue;
public double val1;
public double val2;
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mLatRef = mRootRef.child("lat");
DatabaseReference mLongRef = mRootRef.child("long");
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
protected void onStart() {
super.onStart();
mLatRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Latvalue = dataSnapshot.getValue(String.class);
val1 = Double.parseDouble(Latvalue);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mLongRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Longvalue = dataSnapshot.getValue(String.class);
val2 = Double.parseDouble(Longvalue);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(val1 , val2);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
错误:
--------- beginning of crash
03-02 00:34:40.189 11487-11487/com.example.ashutosh.userappnew E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ashutosh.userappnew, PID: 11487
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.android.gms.internal.zzbtg.zzaF(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.ashutosh.userappnew.MapsActivity$1.onDataChange(MapsActivity.java:50)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5300)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-02 00:34:40.190 11487-11487/com.example.ashutosh.userappnew D/AppTracker: App Event: crash
03-02 00:34:40.291 11487-11487/com.example.ashutosh.userappnew I/Process: Sending signal. PID: 11487 SIG: 9
答案 0 :(得分:1)
答案在你的堆栈跟踪中:
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
您正在尝试从数据库中读取一个Long作为字符串,这会导致异常。
Latvalue = dataSnapshot.getValue(String.class);
val1 = Double.parseDouble(Latvalue);
...
Longvalue = dataSnapshot.getValue(String.class);
val2 = Double.parseDouble(Longvalue);
在这里,您正在阅读LatValue
和LongValue
作为字符串,所以大概它们实际上是Longs。我不确定你为什么要将它们存储或读作字符串,因为你无论如何都要立即将它们转换为Double。只需将默认值转换为double:
val1 = (double)dataSnapshot.getValue();
...
val2 = (double)dataSnapshot.getValue();
请参阅reading and writing data types上的Firebase文档。