android - Add Item from EditText to RecyclerView -
i making project college. has notice page can post notice. notice page has recyclerview notices posted can viewed users using app, if text stuck permanent page. notice gets saved in database successfully, dont want show notice database, project purpose. ever time on clicking post button app force closes. wrong in code. please help. below files.
activity_notice.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_width="250dp" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="recent notices" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:textcolor="#ff0612" android:textsize="30dp"/> <edittext android:layout_width="wrap_content" android:layout_height="50dp" android:id="@+id/noticetext" android:hint="post notice" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignright="@+id/textview" android:layout_alignend="@+id/textview" /> <imagebutton android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/sndnoticebut" android:src="@drawable/send1" android:background="@null" android:layout_alignparentbottom="true" android:layout_torightof="@+id/textview" android:layout_toendof="@+id/textview" android:onclick="additem"/> </relativelayout> <android.support.v7.widget.recyclerview android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="155dp" android:scrollbars="vertical" android:layout_margintop="70dp" android:onclick="additem" /> </relativelayout>
noticeview.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="match_parent" android:layout_height="155dp" android:id="@+id/noticetext" android:layout_margintop="70dp"/> </linearlayout>
notice.java
package com.salesiancollege.www.salesiani_smart; import android.app.activity; import android.app.taskstackbuilder; import android.os.asynctask; import android.os.bundle; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.graphics.typeface; import android.os.message; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.app.notificationcompat; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.imagebutton; import android.widget.textview; import android.widget.toast; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist; import java.util.list; import static java.lang.system.currenttimemillis; public class notice extends activity { imagebutton notbut; edittext nottxt; string post; context ctx=this; private recyclerview mrecyclerview; private customrecycleradapter madapter; private recyclerview.layoutmanager mlayoutmanager; private list<data> mdata = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_notice); notbut = (imagebutton) findviewbyid(r.id.sndnoticebut); nottxt = (edittext) findviewbyid(r.id.noticetext); mrecyclerview = (recyclerview) findviewbyid(r.id.recycler); // if size of views not change data changes. mrecyclerview.sethasfixedsize(true); // setting layoutmanager. mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); } public void additem(view v) { post = nottxt.gettext().tostring(); // add data locally list. data datatoadd = new data(nottxt.gettext().tostring()); mdata.add(datatoadd); // update adapter. madapter.additem(mdata.size()-1, datatoadd); background b = new background(); b.execute(post); } class background extends asynctask<string, string, string> { @override protected string doinbackground(string... params) { string nottxt = params[0]; string data=""; int tmp; try { url url = new url("http://10.0.2.2/salesiancollege/notice.php"); string urlparams = "nottxt="+nottxt; httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setdooutput(true); outputstream os = httpurlconnection.getoutputstream(); os.write(urlparams.getbytes()); os.flush(); os.close(); inputstream = httpurlconnection.getinputstream(); while((tmp=is.read())!=-1){ data+= (char)tmp; } is.close(); httpurlconnection.disconnect(); return data; } catch (malformedurlexception e) { e.printstacktrace(); return "exception: "+e.getmessage(); } catch (ioexception e) { e.printstacktrace(); return "exception: "+e.getmessage(); } } @override protected void onpostexecute(string s) { if(s.equals("")){ s="notice posted."; } toast.maketext(ctx, s, toast.length_long).show(); } } }
data.java
package com.salesiancollege.www.salesiani_smart; import android.app.activity; /** * created partho on 11-06-2016. */ public class data { public string text; public data(string text) { this.text = text; } }
customrecycleradapter.java
package com.salesiancollege.www.salesiani_smart; import android.graphics.color; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import java.util.collections; import java.util.list; public class customrecycleradapter extends recyclerview.adapter<recyclerviewholder> { private list<data> mdata = collections.emptylist(); public customrecycleradapter() { // pass context or other static stuff needed. } public void updatelist(list<data> data) { mdata = data; notifydatasetchanged(); } public void additem(int position, data data) { mdata.add(position, data); notifyiteminserted(position); } @override public recyclerviewholder oncreateviewholder(viewgroup viewgroup, int position) { layoutinflater inflater = layoutinflater.from(viewgroup.getcontext()); view itemview = inflater.inflate(r.layout.noticeview, viewgroup, false); return new recyclerviewholder(itemview); } @override public void onbindviewholder(recyclerviewholder viewholder, int position) { viewholder.notice.settext(mdata.get(position).text); } @override public int getitemcount() { return mdata.size(); } }
recyclerviewholder.java
package com.salesiancollege.www.salesiani_smart; import android.support.v7.widget.recyclerview; import android.view.view; import android.widget.textview; public class recyclerviewholder extends recyclerview.viewholder { public textview notice; public recyclerviewholder(view itemview) { super(itemview); notice = (textview) itemview.findviewbyid(r.id.noticetext); } }
logcat:
06-11 15:54:05.650 335-722/? v/windowmanager﹕ adding window window{7b97cc0 u0 com.salesiancollege.www.salesiani_smart/com.salesiancollege.www.salesiani_smart.notice} @ 3 of 8 (after window{293060cb u0 com.salesiancollege.www.salesiani_smart/com.salesiancollege.www.salesiani_smart.studentdashboard}) 06-11 15:54:05.959 1291-1309/? w/egl_emulation﹕ eglsurfaceattrib not implemented 06-11 15:54:05.960 1291-1309/? w/openglrenderer﹕ failed set egl_swap_behavior on surface 0xb4b7cb80, error=egl_success 06-11 15:54:06.087 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:06.751 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:06.783 335-358/? i/activitymanager﹕ displayed com.salesiancollege.www.salesiani_smart/.notice: +2s147ms 06-11 15:54:13.690 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:13.895 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:14.209 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:14.364 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:14.562 1291-1291/? e/recyclerview﹕ no adapter attached; skipping layout 06-11 15:54:23.115 335-383/? w/audiotrack﹕ audio_output_flag_fast denied client 06-11 15:54:23.131 1291-1291/? d/androidruntime﹕ shutting down vm 06-11 15:54:23.182 1291-1291/? e/androidruntime﹕ fatal exception: main process: com.salesiancollege.www.salesiani_smart, pid: 1291 java.lang.illegalstateexception: not execute method of activity @ android.view.view$1.onclick(view.java:4020) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ android.view.view$1.onclick(view.java:4015) at android.view.view.performclick(view.java:4780) at android.view.view$performclick.run(view.java:19866) at android.os.handler.handlecallback(handler.java:739) at android.os.handler.dispatchmessage(handler.java:95) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5257) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) at com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void com.salesiancollege.www.salesiani_smart.customrecycleradapter.additem(int, com.salesiancollege.www.salesiani_smart.data)' on null object reference @ com.salesiancollege.www.salesiani_smart.notice.additem(notice.java:83) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at android.view.view$1.onclick(view.java:4015) at android.view.view.performclick(view.java:4780) at android.view.view$performclick.run(view.java:19866) at android.os.handler.handlecallback(handler.java:739) at android.os.handler.dispatchmessage(handler.java:95) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5257) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) at com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
Comments
Post a Comment