错误我想再次发送该对象,我的代码想将其转换为String
,请帮忙,错误是:
com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为类型DetailRestaurant
public class UploadImage extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef;
private EditText nameImageR;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_image);
myRef = database.getReference("").child("users");
nameImageR = findViewById(R.id.nameImageR);
Button map = findViewById(R.id.buttonUpload);
map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
writeNewImage();
Intent Client= new Intent(UploadImage.this, MainRestaurantActivity.class);
startActivity(Client);
finish();
}
});
}
private void writeNewImage() {
myRef = database.getReference("users").child("restaurant").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
ValueEventListener valueEventListener = myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
DetailRestaurant value1 = areaSnapshot.getValue(DetailRestaurant.class);
value1.setImageRestaurant(nameImageR.getText().toString());
if (value1.getUid().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
String userIDR = value1.getUid();
myRef = database.getReference("");
myRef.child("users").child("restaurant").child(userIDR).setValue(value1);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Eror", "Something happened");
}
});
}
}
public class DetailRestaurant implements Serializable {
private String name;
private String uid;
private String ImageRestaurant;
private HashMap<String, DetailPlate> plates;
public DetailRestaurant(){
this.plates = new HashMap<String, DetailPlate>();
}
public DetailRestaurant(String ImageRestaurant){
this();
this.ImageRestaurant = ImageRestaurant;
}
public DetailRestaurant(String name, String uid) {
this();
this.name = name;
this.uid = uid;
}
public DetailRestaurant(String name, String uid, String ImageRestaurant, HashMap<String, DetailPlate> plates) {
this.name = name;
this.uid=uid;
this.ImageRestaurant = ImageRestaurant;
this.plates = plates;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getImageRestaurant() {
return ImageRestaurant;
}
public void setImageRestaurant(String imageRestaurant) {
ImageRestaurant = imageRestaurant;
}
public void setPlates(HashMap<String, DetailPlate> plates) {
this.plates = plates;
}
public HashMap<String, DetailPlate> getPlates(){ return plates; }
public ArrayList<DetailPlate> getPlatesList() {
ArrayList<DetailPlate> plates = new ArrayList<DetailPlate>();
for (DetailPlate p : this.plates.values()){
plates.add(p);
}
return plates;
}
@Override
public String toString() {
return this.getName();
}
}
E / AndroidRuntime:致命异常:主要 处理:com.example.fernandalopezcardenas.uneatfinal,PID:9713 com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.fernandalopezcardenas.uneatfinal.Detail.DetailRestaurant 在com.google.android.gms.internal.zzbtg.zze(未知来源) 在com.google.android.gms.internal.zzbtg.zzb(未知来源) 在com.google.android.gms.internal.zzbtg.zza(未知来源) 在com.google.firebase.database.DataSnapshot.getValue(未知来源) at com.example.fernandalopezcardenas.uneatfinal.Restaurant.UploadImage $ 2.onDataChange(UploadImage.java:56) 在com.google.android.gms.internal.zzbpx.zza(未知来源) 在com.google.android.gms.internal.zzbqx.zzZT(未知来源) 在com.google.android.gms.internal.zzbra $ 1.run(未知来源) 在android.os.Handler.handleCallback(Handler.java:746) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5459) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:0)
有同样的问题,你需要对第一个数据快照进行另一次内部迭代。试试这个..
for (DataSnapshot areaSnapshotOne : dataSnapshot.getChildren())
{
for (DataSnapshot areaSnapshotTwo : areaSnapshotOne.getChildren())
{
DetailRestaurant value1 = areaSnapshotTwo.getValue(DetailRestaurant.class);
Log.d("details",value1.toString());
/*your rest code here*/
}
}