2017-03-01 133 views
-3

代码:Android应用程序崩溃时,谷歌地图连接火力

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

[Firebase DatabaseException:无法将java.lang.Long类型的值转换为String](http://stackoverflow.com/questions/39552348/firebase-databaseexception-failed-to-convert-value-of -type-java-lang-long-to-st) – SMR

回答

1

答案就在你的堆栈跟踪:

com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String 

你试图从数据库中读取一个Long作为String,并导致异常。

Latvalue = dataSnapshot.getValue(String.class); 
val1 = Double.parseDouble(Latvalue); 
... 
Longvalue = dataSnapshot.getValue(String.class); 
val2 = Double.parseDouble(Longvalue); 

在这里,你正在阅读LatValueLongValue为字符串,因此推测它们实际上是多头。我不确定你为什么要将它们作为字符串存储或读取,因为无论如何你都立即将它们转换为Double。只需将默认值加倍即可:

val1 = (double)dataSnapshot.getValue(); 
... 
val2 = (double)dataSnapshot.getValue(); 

请参阅Firebase文档reading and writing data types

+0

谢谢Travis!我做了您所建议的更改,现在应用程序不会崩溃。但它仍然无法检索值。 –