我正在尝试从Firebase获取当前ID位置,并发送到另一个类以从Firebase检索相关ID信息,但是我遇到以下错误,请告诉我该怎么做?
获取纬度和经度的空值?在此处输入图像描述


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) {

        }
    });



}
}


我的Firebase视图是

java - 如何将值从一类的onOptionsItemSelected方法传递给另一类-LMLPHP

最佳答案

使用原始数据类型double latitude = ............

要么

Double latitude = new Double(dataSnapshot.child("latitude").getValue(Double.class));

09-10 09:41
查看更多