点击客户名称后,他/她的位置需要在Google地图上标记。当我在弹出窗口中显示客户详细信息时,客户的纬度和经度将通过意图从弹出窗口类传递到地图活动类作为对象。在map activity.java中接收它时,它变为null并抛出空指针异常。
弹出窗口类的代码(CustomerPupUp.java):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_pop_up);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width),(int)(height*.7));
listView = (ListView)findViewById(R.id.custListVw);
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.activity_cust_layout,R.id.cus, list);
customer = new AddCustomer();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren() )
{
customer = ds.getValue(AddCustomer.class);
Log.d("CustomerPopUp", "onDataChange: " + customer.getLat());
list.add(customer.getFname().toString() + " " + customer.getLname().toString() );
customerList.add(customer);
}
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(CustomerPopUp.this, MapsActivity.class);
intent.putExtra("CustomerData", customerList.get(position));
startActivity(intent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
以下代码段显示了我如何通过MapsActivity.java中的意图获取对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
try {
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
customer= (AddCustomer) getIntent().getSerializableExtra("CustomerData");
mMap.addMarker(new MarkerOptions().position(new LatLng(customer.getLat(), customer.getLng())).title(customer.getFname() + customer.getLname()));
}catch (NullPointerException ex)
{
Log.d(TAG, "onCreate: " + ex.getMessage());
}
search = (EditText)findViewById(R.id.SearchText);
gps =(ImageView) findViewById(R.id.GpsBtn);
cust = (ImageView)findViewById(R.id.CusBtn);
isServiceFine();
getLocationPermission();
cust.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MapsActivity.this, CustomerPopUp.class);
startActivity(intent);
}
});
}
以下是AddCustomer类
public class AddCustomer implements Serializable {
private String fname;
private String lname;
private String email;
private String pass;
private String confPass;
private int num;
Double lat;
Double lng;
AddCustomer()
{
}
public AddCustomer(String FName, String LName, String EMail, String PWD, String CONFPWD, int NUM, Double latitude, Double longtitude)
{
this.fname = FName;
this.lname = LName;
this.email = EMail;
this.pass = PWD;
this.confPass = CONFPWD;
this.num = NUM;
this.lat = latitude;
this.lng = longtitude;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getConfPass() {
return confPass;
}
public void setConfPass(String confPass) {
this.confPass = confPass;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
}
请帮我解决这个问题。
答案 0 :(得分:1)
您只能通过Parcelable
传递Serializable
或Intent
个对象。在这种情况下,使用自定义对象,您需要将其设为Parcelable
或Serializable
。
我建议您使用http://www.parcelabler.com/轻松生成代码。
答案 1 :(得分:0)
mMap.addMarker(new MarkerOptions()。position(new 经纬度(customer.getLat() customer.getLng()))。title(customer.getFname()+ customer.getLname()));
这就是你的问题所在。而不是使用匿名对象,声明MarkerOptions
和LatLng
的类变量并使用它如下:
public class Foo extends FragmentActivity implements OnMapReadyCallback {
private MarkerOptions mrkOpt = new MarkerOptions();
private LatLng lalg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
try {
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
customer= (AddCustomer) getIntent().getSerializableExtra("CustomerData");
lalg = new LatLng(customer.getLat(), customer.getLng());
mrkOpt.position(lalg);
mrkOpt.title(customer.getFname() + customer.getLname())
mMap.addMarker(mrkOpt);
}catch (NullPointerException ex)
{
Log.d(TAG, "onCreate: " + ex.getMessage());
}
search = (EditText)findViewById(R.id.SearchText);
gps =(ImageView) findViewById(R.id.GpsBtn);
cust = (ImageView)findViewById(R.id.CusBtn);
isServiceFine();
getLocationPermission();
cust.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MapsActivity.this, CustomerPopUp.class);
startActivity(intent);
}
});
}
}
答案 2 :(得分:0)
你能试试吗?移除setAdapter
内的onItemClickListener
和onDataChange
内的onCreate
并将其放在onDataChange
上..然后在notifyDataSetChanged
检查适配器是否为空,如果没有尝试做 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_pop_up);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width),(int)(height*.7));
listView = (ListView)findViewById(R.id.custListVw);
database = FirebaseDatabase.getInstance();
reference = database.getReference("Customers");
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.activity_cust_layout,R.id.cus, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(customerList.size() != 0) {
Intent intent = new Intent(CustomerPopUp.this, MapsActivity.class);
intent.putExtra("CustomerData", customerList.get(position));
startActivity(intent);
}
}
});
customer = new AddCustomer();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren() )
{
customer = ds.getValue(AddCustomer.class);
Log.d("CustomerPopUp", "onDataChange: " + customer.getLat());
list.add(customer.getFname().toString() + " " + customer.getLname().toString() );
customerList.add(customer);
if(adapter != null)
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我不知道这是否有效,但我希望你能了解情况。我认为可能的问题是你没有传递任何数据,这是Null异常的原因。
.animate()