java - When I try to Parse my JSON to a list view, I receive a Resource not found exception? -
this question has answer here:
error below keep receiving when trying parse json
info listview
.
09-10 19:31:02.560 11200-11200/com.example.aids.a09application e/androidruntime: fatal exception: main process: com.example.aids.a09application, pid: 11200 android.content.res.resources$notfoundexception: string resource id #0x1 @ android.content.res.resources.gettext(resources.java:1178) @ android.widget.textview.settext(textview.java:5157) @ com.example.aids.a09application.standingsadapter.getview(standingsadapter.java:65) @ android.widget.abslistview.obtainview(abslistview.java:3229) @ android.widget.listview.measureheightofchildren(listview.java:1396) @ android.widget.listview.onmeasure(listview.java:1303) @ android.view.view.measure(view.java:21046) @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:715) @ android.widget.relativelayout.onmeasure(relativelayout.java:461) @ android.view.view.measure(view.java:21046) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6460) @ android.widget.framelayout.onmeasure(framelayout.java:185) @ android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:139) @ android.view.view.measure(view.java:21046) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6460) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1464) @ android.widget.linearlayout.measurevertical(linearlayout.java:758) @ android.widget.linearlayout.onmeasure(linearlayout.java:640) @ android.view.view.measure(view.java:21046) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6460) @ android.widget.framelayout.onmeasure(framelayout.java:185) @ android.view.view.measure(view.java:21046) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6460) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1464) @ android.widget.linearlayout.measurevertical(linearlayout.java:758) @ android.widget.linearlayout.onmeasure(linearlayout.java:640) @ android.view.view.measure(view.java:21046) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6460) @ android.widget.framelayout.onmeasure(framelayout.java:185) @ com.android.internal.policy.decorview.onmeasure(decorview.java:785) @ android.view.view.measure(view.java:21046) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2562) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1629) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1878) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1509) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:7051) @ android.view.choreographer$callbackrecord.run(choreographer.java:927) @ android.view.choreographer.docallbacks(choreographer.java:702) @ android.view.choreographer.doframe(choreographer.java:638) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:913) @ 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:6692) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1468) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1358)
the logcat
above states error line 65 of standingsadapter
class, below. line 66 standingsholder.txt_id.settext( standings.getteam_id() );
public class standingsadapter extends arrayadapter { list list = new arraylist(); public standingsadapter(@nonnull context context, @layoutres int resource) { super( context, resource ); } public void add(standings object) { super.add( object ); list.add( object ); } @override public int getcount() { return list.size(); } @nullable @override public object getitem(int position) { return list.get( position ); } @nonnull @override public view getview(int position, @nullable view convertview, @nonnull viewgroup parent) { view row; row = convertview; standingsholder standingsholder; if(row ==null){ layoutinflater layoutinflater= (layoutinflater) this.getcontext().getsystemservice( context.layout_inflater_service ); row = layoutinflater.inflate(r.layout.row_layout, parent, false); standingsholder = new standingsholder(); standingsholder.tx_id = (textview) row.findviewbyid( r.id.sec_1 ); standingsholder.txt_id = (textview) row.findviewbyid( r.id.sec_2 ); standingsholder.tx_fn = (textview) row.findviewbyid( r.id.sec_3 ); standingsholder.tx_ln = (textview) row.findviewbyid( r.id.sec_4 ); standingsholder.tx_pos = (textview) row.findviewbyid( r.id.sec_5 ); standingsholder.tx_po = (textview) row.findviewbyid( r.id.sec_6 ); row.settag( standingsholder ); } else { standingsholder = (standingsholder) row.gettag(); } standings standings = (standings)this.getitem( position ); standingsholder.tx_id.settext( standings.getdriver_id() ); standingsholder.txt_id.settext( standings.getteam_id() ); standingsholder.tx_fn.settext( standings.getfirstname() ); standingsholder.tx_ln.settext( standings.getlastname() ); standingsholder.tx_pos.settext( standings.getposition() ); standingsholder.tx_po.settext( standings.getpoints() ); return row; } static class standingsholder { textview tx_id, txt_id, tx_fn, tx_ln, tx_pos, tx_po; } }
i post other classes android application, involved in error. below standings class
public class standings { private int driver_id; private int team_id; private string firstname; private string lastname; private int position; private int points; public standings(int driver_id, int team_id, string firstname, string lastname, int position, int points) { this.setdriver_id( driver_id ); this.setteam_id( team_id ); this.setfirstname( firstname ); this.setlastname( lastname ); this.setposition( position ); this.setpoints( points ); } public int getdriver_id() { return driver_id; } public void setdriver_id(int driver_id) { this.driver_id = driver_id; } public int getteam_id() { return team_id; } public void setteam_id(int team_id) { this.team_id = team_id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public int getposition() { return position; } public void setposition(int position) { this.position = position; } public int getpoints() { return points; } public void setpoints(int points) { this.points = points; } }
below standinglist fragment
main fragment
buttons download json
, parse json
found.
public class standingslist extends fragment implements view.onclicklistener { //make member variable views button mbutton; button mbutton1; textview mresult; string json_response; string json_string; progressdialog mprogressdialog; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate( r.layout.fragment_standings, container, false ); //get reference of views mbutton = (button) view.findviewbyid( r.id.button ); mbutton1 = (button) view.findviewbyid( r.id.buttontwo ); mresult = (textview) view.findviewbyid( r.id.result ); mbutton1.setonclicklistener(this); //when button clicked mbutton.setonclicklistener( new view.onclicklistener() { @override public void onclick(view view) { //call getjsonresponse method , fetch response server new getjsonresponse().execute(); } } ); return view; } public class getjsonresponse extends asynctask<void, void, string> { string serverurl; public getjsonresponse() { mprogressdialog = new progressdialog( getactivity() ); mprogressdialog.setmessage( "please wait" ); mprogressdialog.settitle( "processing" ); mprogressdialog.setcancelable( false ); } @override protected void onpreexecute() { //set url have fetch json response serverurl = "http://163.172.142.145/get_info.php"; mprogressdialog.show(); } @override protected string doinbackground(void... params) { try { url url = new url( serverurl ); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader( new inputstreamreader( inputstream ) ); stringbuilder stringbuilder = new stringbuilder(); while ((json_response = bufferedreader.readline()) != null) { stringbuilder.append( json_response + "\n" ); } inputstream.close(); bufferedreader.close(); httpurlconnection.disconnect(); return stringbuilder.tostring().trim(); } catch (malformedurlexception e) { log.e( tag, "malformedurlexception: " + e ); //print exception message log } catch (ioexception e) { log.e( tag, "ioexception: " + e ); //print exception message log } return null; } @override protected void onprogressupdate(void... values) { super.onprogressupdate( values ); } @override protected void onpostexecute(string result) { //set result returned doinbackground() method result textview mresult.settext( result ); mprogressdialog.dismiss(); json_string = result; } } public void onclick(view view){ switch (view.getid()) { case r.id.buttontwo: intent intent = new intent(getactivity(), displaylistview.class); intent.putextra( "json_data",json_string ); startactivity( intent ); break; } } }
the last class
have works alongside json
parse display in list view clas
s, meant named. here below:
public class displaylistview extends appcompatactivity { string json_string; jsonobject jsonobject; jsonarray jsonarray; standingsadapter standingsadapter; listview listview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate( savedinstancestate ); setcontentview( r.layout.display_list_view_layout ); listview= (listview) findviewbyid( r.id.listviewparse ); standingsadapter = new standingsadapter(this,r.layout.row_layout); listview.setadapter( standingsadapter ); json_string = getintent().getextras().getstring( "json_data" ); try { jsonarray = new jsonobject(json_string).getjsonarray("server_response"); int count = 0; int driver_id, team_id, position, points; string firstname, lastname; while(count<jsonarray.length()) { jsonobject jo = jsonarray.getjsonobject(count); driver_id = jo.getint( "driver_id" ); team_id =jo.getint( "team_id" ); firstname = jo.getstring( "first_name" ); lastname= jo.getstring( "last_name" ); position= jo.getint( "position" ); points = jo.getint( "points" ); standings standings = new standings(driver_id, team_id, firstname, lastname, position, points ); standingsadapter.add( standings ); count++; } } catch (jsonexception e) { e.printstacktrace(); } } }
you need convert int values string using integer.tostring(intargument);
standings standings = (standings)this.getitem( position ); standingsholder.tx_id.settext( integer.tostring(standings.getdriver_id()) ); standingsholder.txt_id.settext(integer.tostring(standings.getteam_id())); standingsholder.tx_fn.settext( standings.getfirstname() ); standingsholder.tx_ln.settext( standings.getlastname() ); standingsholder.tx_pos.settext(integer.tostring(standings.getposition())); standingsholder.tx_po.settext(integer.tostring(standings.getpoints()));
Comments
Post a Comment