[英]Android location app crashing after add Firebase
我嘗試使用 Android 工作室在 Google map 上創建位置標記,並從 Firebase 實時數據庫獲取位置。 在我將 Firebase 實時數據庫添加到我的應用程序后,它一直在崩潰。 請幫忙。 這是 MapsActivity.JAVA 代碼
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
String latitude,longitude,value;
int n,n1;
float x,y;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("device");
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
value = dataSnapshot.getValue().toString();
n = value.indexOf("attitude=0");
latitude = value.substring(n+10,n+17);
longitude = value.substring(n+28,n1+34);
x = Float.parseFloat(latitude);
y = Float.parseFloat(longitude);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// 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);
};
/**
* 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;
// x = (float) 13.9365;
// y = (float) 100.4991;
x = Float.parseFloat(latitude);
y = Float.parseFloat(longitude);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(x, y);
mMap.addMarker(new MarkerOptions().position(sydney).title("Device1"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}
這似乎是個問題
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smart, PID: 13831
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at com.example.smartbin.MapsActivity.onMapReady(MapsActivity.java:98)
at com.google.android.gms.maps.zzak.zza(Unknown Source:2)
at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:12)
at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:12)
at android.os.Binder.transact(Binder.java:667)
at cx.b(:com.google.android.gms.dynamite_mapsdynamite@201817068@20.18.17 (100700-0):2)
at com.google.maps.api.android.lib6.impl.be.run(:com.google.android.gms.dynamite_mapsdynamite@201817068@20.18.17 (100700-0):2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
路徑是唯一鍵我不知道如何創建路徑,所以我嘗試 substring 並將其變成浮點數。 謝謝
似乎x = Float.parseFloat(latitude)
崩潰了,因為緯度是 null。 你在這條線上初始化緯度的值latitude = value.substring(n+10,n+17);
, 這只會在有子添加到 FirebaseDatabase 時運行,所以當onMapReady
被調用時,沒有子添加到數據庫,所以緯度是 null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.