我在收到Firebase时遇到此错误,请在哪里出错。我需要你的帮助我是Firebase的新手。我真的不知道什么是错的,代码在我看来是正常的。
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.murilo.firebaseimagemtexo, PID: 11124
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.murilo.firebaseimagemtexo.Movie
at com.google.android.gms.internal.zzanp.zze(Unknown Source)
at com.google.android.gms.internal.zzanp.zzb(Unknown Source)
at com.google.android.gms.internal.zzanp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.murilo.firebaseimagemtexo.BdFire.fetechUpdate(BdFire.java:84)
at com.example.murilo.firebaseimagemtexo.BdFire$1.onChildAdded(BdFire.java:50)
at com.google.android.gms.internal.zzajg.zza(Unknown Source)
at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source)
at com.google.android.gms.internal.zzalj$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
课堂电影:
public class Movie {
private String nome;
private String url;
public Movie() {
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}}
BdFire类:
public class BdFire {
Context c;
ListView lv;
ArrayList<Movie>movies = new ArrayList<>();
CustomAdapter adapter;
FirebaseDatabase database;
DatabaseReference myRef;
public BdFire(Context c, ListView lv) {
this.c = c;
this.lv = lv;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Movie");
}
//save
public void saveOnline(String nome,String url){
Movie m = new Movie();
m.setNome(nome);
m.setUrl(url);
myRef.push().setValue(m);
}
//retrive
public void refreshDate(){
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetechUpdate(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetechUpdate(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void fetechUpdate(DataSnapshot dataSnapshot){
movies.clear();
for (DataSnapshot ds:dataSnapshot.getChildren())
{
Movie m = new Movie();
System.out.println(ds.getValue());
m.setNome(ds.getValue(Movie.class).getNome());
m.setUrl(ds.getValue(Movie.class).getUrl());
movies.add(m);
}
if (movies.size()>0){
adapter = new CustomAdapter(c,movies);
lv.setAdapter(adapter);
}
}}
Class CustmAdapter:
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Movie> movies;
LayoutInflater inflater;
public CustomAdapter(Context c, ArrayList<Movie> movies) {
this.c = c;
this.movies = movies;
}
@Override
public int getCount() {
return movies.size();
}
@Override
public Object getItem(int position) {
return movies.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int postion, View convertView, ViewGroup viewGroup) {
if (inflater == null){
inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView == null){
convertView = inflater.inflate(R.layout.model,viewGroup,false);
}
MyViewHolder holder = new MyViewHolder(convertView);
holder.nomeTxt.setText(movies.get(postion).getNome());
PicassoCliente.downloadImage(c,movies.get(postion).getUrl(),holder.img);
return convertView;
}}
答案 0 :(得分:1)
saveOnline()
:myRef.push().setValue(m)
中的此声明表明myRef
的孩子是Movie
的实例。在refreshDate()
中,您向ChildEventListener
添加了myRef
。 onChildAdded()
的数据快照为Movie
。在fetechUpdate
中,您无法循环数据快照的子项以构建电影列表。孩子是Movie
的字符串成员。