【问题标题】:Android App crashes when Google maps connected to Firebase当 Google 地图连接到 Firebase 时,Android 应用程序崩溃
【发布时间】:2017-03-01 19:32:01
【问题描述】:

代码:

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

【问题讨论】:

标签: android google-maps firebase firebase-realtime-database


【解决方案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);

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

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

请参阅 reading and writing data types 上的 Firebase 文档。

【讨论】:

  • 谢谢特拉维斯!我做了你建议的更改,现在应用程序没有崩溃。但它仍然无法检索值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多