Я пытаюсь получить текущую позицию идентификатора из firebase и отправить другому классу для получения связанной информации об идентификаторе из firebase, но я получаю следующую ошибку, расскажите, как это сделать? Получите нулевое значение широты и долготы ??? введите описание изображения здесь
java .lang.NullPointerException: попытка вызвать виртуальный метод 'double java .lang.Double.doubleValue ()' для ссылки на нулевой объект в com.example.medicalstore.MapRetrieveActivity $ 1.onDataChange (MapRetrieveActivity. java: 62)
public class SupplierNewMedicineList extends AppCompatActivity {
private RecyclerView productsList;
RecyclerView.LayoutManager layoutManager;
private DatabaseReference cartListRef;
private String userID="";
String uID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplier_new_medicine_list);
userID=getIntent().getStringExtra("uid");
productsList=findViewById(R.id.product_list5);
productsList.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
productsList.setLayoutManager(layoutManager);
cartListRef= FirebaseDatabase.getInstance().getReference()
.child("order_list")
.child(userID);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab5);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.supplier_menu, menu);
return true;
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Data> options=
new FirebaseRecyclerOptions.Builder<Data>()
.setQuery(cartListRef, Data.class)
.build();
FirebaseRecyclerAdapter<Data, SupplierViewHolder> adapter=new FirebaseRecyclerAdapter<Data, SupplierViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull SupplierViewHolder holder, int position, @NonNull Data model) {
uID=getRef(position).getKey();
holder.txtMName.setText(model.getmName());
holder.txtMQuantity.setText(model.getmQuantity());
}
@NonNull
@Override
public SupplierViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout,parent,false);
SupplierViewHolder holder=new SupplierViewHolder(view);
return holder;
}
};
productsList.setAdapter(adapter);
adapter.startListening();
}
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(SupplierNewMedicineList.this,SupplierNewOrders.class);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.nav_map:
Intent intent = new Intent(SupplierNewMedicineList.this, MapRetrieveActivity.class);
intent.putExtra("uiid",uID);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Другой класс:
public class MapRetrieveActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String userID="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_retrieve);
// 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;
userID=getIntent().getStringExtra("uiid");
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Current Location").child(userID);
ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Double latitude = dataSnapshot.child("latitude").getValue(Double.class);
Double longitude = dataSnapshot.child("longitude").getValue(Double.class);
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title("Marker Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 14F));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
My Представление Firebase: