我正在尝试检索每个特定帖子(纬度和经度)的位置,并且我通过以下方式进行操作,请参见下面的代码。我不明白为什么会收到此错误:
无法将类型java.lang.String的对象转换为com.e.events.Model.Post类型
我已经在其他活动中使用了此代码,但效果很好。
我需要做的是,因为每个帖子都有自己的特定postid
,我想将其存储在SharedPreferences中,然后通过Firebase查找特定的postid
,然后返回该帖子的特定位置“纬度和经度”。任何人都可以向我解释为什么我会收到此错误?
MapsActivityUser.java
public class MapsActivityUser extends FragmentActivity implements OnMapReadyCallback {
String postid;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_user);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
postid = preferences.getString("postid", "none");
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts").child(postid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null) {
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
} else {
Toast.makeText(MapsActivityUser.this, "Event doesn't have location on Map", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Post.java
public class Post {
private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Long timestamp;
private double Latitude;
private double Longitude;
private LocationHelper locationHelper;
public Post(String description, String postId, String postImage, String publisher, Long timestamp,
String text_event, String text_location, String text_date_time, double latitude, double longitude, LocationHelper location) {
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
this.text_event = text_event;
this.text_location = text_location;
this.text_date_time = text_date_time;
this.timestamp = timestamp;
Latitude = latitude;
Longitude = longitude;
this.locationHelper = location;
}
public Post() {
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getText_event() {
return text_event;
}
public void setText_event(String text_event) {
this.text_event = text_event;
}
public String getText_location() {
return text_location;
}
public void setText_location(String text_location) {
this.text_location = text_location;
}
public String getText_date_time() {
return text_date_time;
}
public void setText_date_time(String text_date_time) {
this.text_date_time = text_date_time;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
public LocationHelper getLocation() {
return locationHelper;
}
public void setLocation(LocationHelper location) {
this.locationHelper = location;
}
}
LocationHelper.java
public class LocationHelper {
private double Latitude;
private double Longitude;
public LocationHelper(double latitude, double longitude) {
Latitude = latitude;
Longitude = longitude;
}
public LocationHelper() {
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
}
Logcat
处理:com.e.events,PID:22267com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.e.events.Model.Post类型在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database @@ 19.2.0:435)在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database @@ 19.2.0:231)com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database @@ 19.2.0:79)com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database @@ 19.2.0:203)在com.e.events.Map.MapsActivityUser $ 1.onDataChange(MapsActivityUser.java:51)com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 19.2.0:75)
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.e.events.Model.LocationHelper com.e.events.Model.Post.getLocation()'在com.e.events.Map.MapsActivityUser $ 1.onDataChange(MapsActivityUser.java:53)com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 19.2.0:75)com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database @@ 19.2.0:63)com.google.firebase.database.core.view.EventRaiser $ 1.run(com.google.firebase:firebase-database @@ 19.2.0:55)在android.os.Handler.handleCallback(Handler.java:907)在android.os.Handler.dispatchMessage(Handler.java:105)在android.os.Looper.loop(Looper.java:216)在android.app.ActivityThread.main(ActivityThread.java:7625)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:524)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
检查您的查询后,我可以看到您传递了post_id,因此它将直接提供发布数据。您不需要遍历datasnapshot
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(Post.class);
if (post != null) {
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
} else {
Toast.makeText(MapsActivityUser.this, "Event doesn't have location on Map", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Post post = snapshot.getValue(Post.class);
这是您有问题的行,请替换为:
Post post = snapshot.toObject(Post.class);