android - How to solve error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 -
this question has answer here:
this code used work added things contentprovider name[s]. no matter how check there wont null or empty arraylists, still same error.
logcat
05-25 08:57:40.429: e/androidruntime(11600): fatal exception: main 05-25 08:57:40.429: e/androidruntime(11600): process: com.example.sillimaninquirer, pid: 11600 05-25 08:57:40.429: e/androidruntime(11600): java.lang.indexoutofboundsexception: invalid index 0, size 0 05-25 08:57:40.429: e/androidruntime(11600): @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) 05-25 08:57:40.429: e/androidruntime(11600): @ java.util.arraylist.get(arraylist.java:308) 05-25 08:57:40.429: e/androidruntime(11600): @ com.example.sillimaninquirer.adapter.getview(newsfeed.java:402) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.abslistview.obtainview(abslistview.java:2467) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.listview.measureheightofchildren(listview.java:1286) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.listview.onmeasure(listview.java:1194) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5698) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1692) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.linearlayout.measurevertical(linearlayout.java:760) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.linearlayout.onmeasure(linearlayout.java:629) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:846) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.relativelayout.onmeasure(relativelayout.java:512) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5698) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.framelayout.onmeasure(framelayout.java:465) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5698) 05-25 08:57:40.429: e/androidruntime(11600): @ com.android.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:447) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5698) 05-25 08:57:40.429: e/androidruntime(11600): @ android.widget.framelayout.onmeasure(framelayout.java:465) 05-25 08:57:40.429: e/androidruntime(11600): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2728) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.view.measure(view.java:17915) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2412) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1414) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1638) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1292) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6598) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.choreographer$callbackrecord.run(choreographer.java:800) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.choreographer.docallbacks(choreographer.java:603) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.choreographer.doframe(choreographer.java:572) 05-25 08:57:40.429: e/androidruntime(11600): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:786) 05-25 08:57:40.429: e/androidruntime(11600): @ android.os.handler.handlecallback(handler.java:815) 05-25 08:57:40.429: e/androidruntime(11600): @ android.os.handler.dispatchmessage(handler.java:104) 05-25 08:57:40.429: e/androidruntime(11600): @ android.os.looper.loop(looper.java:194) 05-25 08:57:40.429: e/androidruntime(11600): @ android.app.activitythread.main(activitythread.java:5631) 05-25 08:57:40.429: e/androidruntime(11600): @ java.lang.reflect.method.invoke(native method) 05-25 08:57:40.429: e/androidruntime(11600): @ java.lang.reflect.method.invoke(method.java:372) 05-25 08:57:40.429: e/androidruntime(11600): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:959) 05-25 08:57:40.429: e/androidruntime(11600): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:754)
newsfeed.java
import java.util.arraylist; import java.util.arrays; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import com.androidquery.aquery; import com.androidquery.callback.ajaxcallback; import com.androidquery.callback.ajaxstatus; import android.os.bundle; import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import android.widget.spinner; import android.widget.textview; import android.widget.toast; public class newsfeed extends activity { listview feed_listview; edittext search; //button t, a, d, c, k; spinner spinner; aquery aq; progressdialog pd; adapter artadapter; jsonarray articles = new jsonarray(); arraylist<string> artid = new arraylist<string>(); arraylist<string> title = new arraylist<string>(); arraylist<string> content = new arraylist<string>(); arraylist<string> date = new arraylist<string>(); arraylist<string> cpid = new arraylist<string>(); arraylist<string> publisher = new arraylist<string>(); jsonarray contentprovider = new jsonarray(); arraylist<string> cpid = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_news_feed); spinner = (spinner)findviewbyid(r.id.spinner1); aq = new aquery(this); pd = new progressdialog(this); pd.setmessage("loading articles. . ."); pd.setcancelable(false); getsidata(); getcontentprovider(); search = (edittext) findviewbyid(r.id.search); feed_listview = (listview) findviewbyid(r.id.listview_newsfeed); artadapter = new adapter(this,artid, title, content, date, publisher,cpid); feed_listview.setadapter(artadapter); feed_listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { string t = title.get(position); string c = content.get(position); string d = date.get(position); string p = publisher.get(position); string cp = cpid.get(position); intent = new intent(getapplicationcontext(), article.class); i.putextra("t", t); i.putextra("c", c); i.putextra("d", d); i.putextra("p", p); i.putextra("cp", cp); startactivity(i); toast.maketext(getapplicationcontext(), "clicked "+position,toast.length_long).show(); } }); search.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { searchtitle(s.tostring()); if(s.tostring().equals("")){ clearvar(); addarticlestovar(); } } @override public void beforetextchanged(charsequence s, int start, int count,int after) { } @override public void aftertextchanged(editable arg0) { } }); } public void addpref(view v){ intent = new intent(v.getcontext(), addpreference.class); startactivity(i); } public void news(view v){ intent = new intent(v.getcontext(), newsfeed.class); startactivity(i); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.news_feed, menu); return true; } public void getsidata(){ aq.progress(pd).ajax("http://sillimaninquirer.esy.es/tws/sidata.php?key=none", jsonobject.class, new ajaxcallback<jsonobject>(){ @override public void callback(string url, jsonobject jo, ajaxstatus as){ try { if(jo!=null){ articles = jo.getjsonarray("articles"); //toast.maketext(getbasecontext(), "articles: "+articles, toast.length_short).show(); }else{ toast.maketext(getbasecontext(), "error: no internet connection", toast.length_short).show(); } } catch (jsonexception e) { e.printstacktrace(); } addarticlestovar(); } }); } public void getcontentprovider(){ aq.progress(pd).ajax("http://sillimaninquirer.esy.es/tws/sidata.php?key=contentprovider", jsonobject.class, new ajaxcallback<jsonobject>(){ @override public void callback(string url, jsonobject jo, ajaxstatus as){ try { if(jo!=null){ contentprovider = jo.getjsonarray("contentprovider"); //toast.maketext(getbasecontext(), "articles: "+articles, toast.length_short).show(); }else{ toast.maketext(getbasecontext(), "error: no internet connection", toast.length_short).show(); } } catch (jsonexception e) { e.printstacktrace(); } } }); } public void addarticlestovar(){ // articles = manage.getarticles(); for(int x=0; x<articles.length(); x++){ try { jsonobject article = articles.getjsonobject(x); artid.add(article.getstring("id")); title.add(article.getstring("title")); content.add(article.getstring("content")); date.add(article.getstring("pdate")); string pub = article.getstring("pubid"); if(pub=="1"){ publisher.add("stones , pebbles"); } if(pub=="1"){ publisher.add("junior sillimanians"); } if(pub=="1"){ publisher.add("the weekly sillimanian"); } string = article.getstring("cpid"); for(int y=0; y<contentprovider.length(); y++){ try { jsonobject cp = contentprovider.getjsonobject(y); string con = article.getstring("id"); if(a==con){ string fname = cp.getstring("firstname"); string lname = cp.getstring("lastname"); string mi = cp.getstring("mi"); cpid.add(fname+" "+mi+". "+lname); } } catch (jsonexception r) { r.printstacktrace(); } } } catch (jsonexception e) { e.printstacktrace(); } } artadapter.notifydatasetchanged(); log.d("xx"," has art? "+articles); } public void searchtitle(string key){ clearvar(); for(int x=0; x<articles.length(); x++){ try { jsonobject article = articles.getjsonobject(x); string[] splited = article.getstring("title").split(" "); //split on word boundries if(arrays.aslist(splited).contains(key)){ artid.add(article.getstring("id")); title.add(article.getstring("title")); content.add(article.getstring("content")); date.add(article.getstring("pdate")); string pub = article.getstring("pubid"); if(pub=="1"){ publisher.add("stones , pebbles"); } if(pub=="1"){ publisher.add("junior sillimanians"); } if(pub=="1"){ publisher.add("the weekly sillimanian"); } string = article.getstring("cpid"); for(int y=0; y<contentprovider.length(); y++){ try { jsonobject cp = contentprovider.getjsonobject(y); string con = article.getstring("id"); if(a==con){ string fname = cp.getstring("firstname"); string lname = cp.getstring("lastname"); string mi = cp.getstring("mi"); cpid.add(fname+" "+mi+". "+lname); } } catch (jsonexception e) { e.printstacktrace(); } } } } catch (jsonexception e) { e.printstacktrace(); } } artadapter.notifydatasetchanged(); } public void searchdate(string key){ clearvar(); for(int x=0; x<articles.length(); x++){ try { jsonobject article = articles.getjsonobject(x); string[] splited = article.getstring("pdtae").split(" "); //split on word boundries if(arrays.aslist(splited).contains(key)){ artid.add(article.getstring("id")); title.add(article.getstring("title")); content.add(article.getstring("content")); date.add(article.getstring("pdate")); string pub = article.getstring("pubid"); if(pub=="1"){ publisher.add("stones , pebbles"); } if(pub=="1"){ publisher.add("junior sillimanians"); } if(pub=="1"){ publisher.add("the weekly sillimanian"); } string = article.getstring("cpid"); for(int y=0; y<contentprovider.length(); y++){ try { jsonobject cp = contentprovider.getjsonobject(y); string con = article.getstring("id"); if(a==con){ string fname = cp.getstring("firstname"); string lname = cp.getstring("lastname"); string mi = cp.getstring("mi"); cpid.add(fname+" "+mi+". "+lname); } } catch (jsonexception e) { e.printstacktrace(); } } } } catch (jsonexception e) { e.printstacktrace(); } } artadapter.notifydatasetchanged(); } public void clearvar(){ title.clear(); content.clear(); date.clear(); cpid.clear(); } } class adapter extends arrayadapter<string>{ context context; arraylist<string> artid, title, content, date, apubid, cpid; //arraylist<articles> art1, art2; adapter (context context, arraylist<string> artid,arraylist<string> title,arraylist<string> content, arraylist<string> date, arraylist<string> apubid, arraylist<string>cpid){ super(context, r.layout.row,r.id.newsfeed_title, title); this.context = context; this.artid = artid; this.content = content; this.date = date; this.title = title; this.apubid = apubid; this.cpid = cpid; } public view getview(int position, view convertview, viewgroup parent){ string c="", con=""; layoutinflater layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view row = layoutinflater.inflate(r.layout.row, parent, false); textview tv_title = (textview) row.findviewbyid(r.id.newsfeed_title); textview tv_content = (textview) row.findviewbyid(r.id.newsfeed_contentext); textview tv_date = (textview) row.findviewbyid(r.id.newsfeed_date); c = content.get(position); for(int cc=0; cc<50; cc++){ con+=c.charat(cc); } tv_title.settext(title.get(position)); tv_content.settext(apubid.get(position)+"\n"+con); tv_date.settext("by "+cpid.get(position)+" "+date.get(position)); return row; } }
when call
artadapter = new adapter(this,artid, title, content, date, publisher,cpid);
the content array empty. why problem
Comments
Post a Comment