gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:30:11 25 4
gpt4 key购买 nike

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

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);
}
}

}

另一个类是

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 View 是

Firebase Image

最佳答案

使用原始数据类型双纬度=............

或者

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

关于java - 如何将值从一个类的 onOptionsItemSelected 方法传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62060331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com