簡體   English   中英

如何將值從一個 class 的 onOptionsItemSelected 方法傳遞到另一個 class

[英]How to pass value from onOptionsItemSelected method of one class to another class

I m trying to get current id position from firebase and send to another class to retrieve related id info from firebase but im getting following error please tell how to do that? 獲取null的經緯度值???在此處輸入圖片描述

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference at 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);
    }
}

}

另一個 class 是

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 視圖是

Firebase 圖片

使用原始數據類型double latitude =............

或者

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM