android - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String -
i student studying android self. first question. please me.
09-12 01:35:35.723 2355-2355/com.example.jan.sanhakproject e/androidruntime: fatal exception: main process: com.example.jan.sanhakproject, pid: 2355 com.google.firebase.database.databaseexception: can't convert object of type java.lang.string type com.example.jan.sanhakproject.chat @ com.google.android.gms.internal.zzbqi.zze(unknown source) @ com.google.android.gms.internal.zzbqi.zzb(unknown source) @ com.google.android.gms.internal.zzbqi.zza(unknown source) @ com.google.firebase.database.datasnapshot.getvalue(unknown source) @ com.example.jan.sanhakproject.chatactivity$2.onchildadded(chatactivity.java:115) @ com.google.android.gms.internal.zzblz.zza(unknown source) @ com.google.android.gms.internal.zzbnz.zzyj(unknown source) @ com.google.android.gms.internal.zzboc$1.run(unknown source) @ android.os.handler.handlecallback(handler.java:751) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:866) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:756) 09-12 01:35:44.318 2355-2374/com.example.jan.sanhakproject w/dynamitemodule: local module descriptor class com.google.firebase.auth not found.
chat.java
public class chat {
public string email; public string text; public chat() { // default constructor required calls datasnapshot.getvalue(comment.class) } public chat(string text) { this.text = text; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string gettext() { return text; } public void settext(string text) { this.text = text; } }
chatactivity.java
public class chatactivity extends appcompatactivity { string tag = this.getclass().getsimplename(); private databasereference mdatabase; private firebasedatabase firebasedatabase; private recyclerview mrecyclerview; private recyclerview.adapter madapter; private recyclerview.layoutmanager mlayoutmanager; string[] mydataset = {"안녕", "오늘","뭐했어","영화볼래?"}; databasereference databasereference; edittext ettext; button btnsend; string email; list<chat> mchat; firebasedatabase database; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_chat); firebasedatabase = firebasedatabase.getinstance(); databasereference = firebasedatabase.getreference(); //database = firebasedatabase.getinstance(); firebaseuser user = firebaseauth.getinstance().getcurrentuser(); if (user != null) { // name, email address, , profile photo url email = user.getemail(); } intent in = getintent(); final string stchatid = in.getstringextra("frienduid"); ettext = (edittext) findviewbyid(r.id.ettext); btnsend = (button) findviewbyid(r.id.btnsend); btnsend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string sttext = ettext.gettext().tostring(); if (sttext.equals("") || sttext.isempty()) { toast.maketext(chatactivity.this, "내용을 입력해 주세요.", toast.length_short).show(); } else { toast.maketext(chatactivity.this, email + "," + sttext, toast.length_short).show(); calendar c = calendar.getinstance(); simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string formatteddate = df.format(c.gettime()); databasereference = database.getreference("users").child(stchatid).child(formatteddate); hashtable<string, string> chat = new hashtable<string, string>(); chat.put("email", email); chat.put("text", sttext); databasereference.setvalue(chat); ettext.settext(""); } } }); mrecyclerview = (recyclerview) findviewbyid(r.id.my_recycler_view); // use setting improve performance if know changes // in content not change layout size of recyclerview mrecyclerview.sethasfixedsize(true); // use linear layout manager mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); mchat = new arraylist<>(); // specify adapter (see next example) madapter = new myadapter(mchat, email, chatactivity.this); mrecyclerview.setadapter(madapter); databasereference.child("users").child(stchatid).child("chats").addchildeventlistener(new childeventlistener() { @override public void onchildadded(datasnapshot datasnapshot, string s) { chat chat = datasnapshot.getvalue(chat.class); mchat.add(chat); mrecyclerview.scrolltoposition(mchat.size() - 1); madapter.notifyiteminserted(mchat.size() - 1); } @override public void onchildchanged(datasnapshot datasnapshot, string s) { } @override public void onchildremoved(datasnapshot datasnapshot) { } @override public void onchildmoved(datasnapshot datasnapshot, string s) { } @override public void oncancelled(databaseerror databaseerror) { } }); } }
myadapter.java
public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private string[] mdataset; list<chat> mchat; string stemail; context context; // provide reference views each data item // complex data items may need more 1 view per item, , // provide access views data item in view holder public static class viewholder extends recyclerview.viewholder { // each data item string in case public textview mtextview; public viewholder(view itemview) { super(itemview); mtextview = (textview) itemview.findviewbyid(r.id.mtextview); } } // provide suitable constructor (depends on kind of dataset) public myadapter(list<chat> mchat, string email, context context) { this.mchat = mchat; this.stemail = email; this.context = context; } @override public int getitemviewtype(int position) { if (mchat.get(position).getemail().equals(stemail)){ return 1; } else { return 2; } } // create new views (invoked layout manager) @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { // create new view view v; if (viewtype == 1) { v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.right_text_view, parent, false); } else { v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.my_text_view, parent, false); } // set view's size, margins, paddings , layout parameters viewholder vh = new viewholder(v); return vh; } // replace contents of view (invoked layout manager) @override public void onbindviewholder(viewholder holder, final int position) { // - element dataset @ position // - replace contents of view element holder.mtextview.settext(mchat.get(position).gettext()); holder.mtextview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { toast.maketext(context, string.valueof(position), toast.length_short).show(); } }); } // return size of dataset (invoked layout manager) @override public int getitemcount() { return mchat.size(); } }
the exception log indicates code trying value of object chat
, finding string
. error occurs @ statement in onchildadded()
:
chat chat = datasnapshot.getvalue(chat.class);
check database confirm children of /users/stchatid/chats
valid chat
objects. looks listening new chats at: /users/stchatid/chats
:
databasereference.child("users").child(stchatid).child("chats").addchildeventlistener(...)
but writing chats @ location:
databasereference = database.getreference("users").child(stchatid).child(formatteddate);
Comments
Post a Comment