listview - getting java.lang.ArrayIndexOutOfBoundsException in android -
i using pinned-section-listview here , getting following error after scrolling,
07-31 17:52:46.640: e/inputeventreceiver(25288): exception dispatching input event. 07-31 17:52:46.657: e/androidruntime(25288): fatal exception: main 07-31 17:52:46.657: e/androidruntime(25288): java.lang.arrayindexoutofboundsexception: length=2; index=2 07-31 17:52:46.657: e/androidruntime(25288): @ android.widget.abslistview$recyclebin.addscrapview(abslistview.java:6916) 07-31 17:52:46.657: e/androidruntime(25288): @ android.widget.abslistview.trackmotionscroll(abslistview.java:5442) 07-31 17:52:46.657: e/androidruntime(25288): @ android.widget.abslistview.scrollifneeded(abslistview.java:3310) 07-31 17:52:46.657: e/androidruntime(25288): @ android.widget.abslistview.ontouchevent(abslistview.java:3654) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.view.dispatchtouchevent(view.java:7143) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2238) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1935) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2244) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1949) 07-31 17:52:46.657: e/androidruntime(25288): @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:1938) 07-31 17:52:46.657: e/androidruntime(25288): @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1392) 07-31 17:52:46.657: e/androidruntime(25288): @ android.app.activity.dispatchtouchevent(activity.java:2408) 07-31 17:52:46.657: e/androidruntime(25288): @ com.android.internal.policy.impl.phonewindow$decorview.dispatchtouchevent(phonewindow.java:1886) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.view.dispatchpointerevent(view.java:7323) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl.deliverpointerevent(viewrootimpl.java:3560) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:3490) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:4637) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:4597) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl$windowinputeventreceiver.oninputevent(viewrootimpl.java:4749) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.inputeventreceiver.dispatchinputevent(inputeventreceiver.java:171) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.inputeventreceiver.nativeconsumebatchedinputevents(native method) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.inputeventreceiver.consumebatchedinputevents(inputeventreceiver.java:163) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl.doconsumebatchedinput(viewrootimpl.java:4717) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.viewrootimpl$consumebatchedinputrunnable.run(viewrootimpl.java:4771) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.choreographer$callbackrecord.run(choreographer.java:747) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.choreographer.docallbacks(choreographer.java:567) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.choreographer.doframe(choreographer.java:534) 07-31 17:52:46.657: e/androidruntime(25288): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:733) 07-31 17:52:46.657: e/androidruntime(25288): @ android.os.handler.handlecallback(handler.java:615) 07-31 17:52:46.657: e/androidruntime(25288): @ android.os.handler.dispatchmessage(handler.java:92) 07-31 17:52:46.657: e/androidruntime(25288): @ android.os.looper.loop(looper.java:153) 07-31 17:52:46.657: e/androidruntime(25288): @ android.app.activitythread.main(activitythread.java:5034) 07-31 17:52:46.657: e/androidruntime(25288): @ java.lang.reflect.method.invokenative(native method) 07-31 17:52:46.657: e/androidruntime(25288): @ java.lang.reflect.method.invoke(method.java:511) 07-31 17:52:46.657: e/androidruntime(25288): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:821) 07-31 17:52:46.657: e/androidruntime(25288): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:584) 07-31 17:52:46.657: e/androidruntime(25288): @ dalvik.system.nativestart.main(native method) package com.benego.fragment; import java.io.file; import java.io.filedescriptor; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import android.content.context; import android.media.mediaplayer; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmenttransaction; import android.text.androidcharacter; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imagebutton; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import com.benego.appcache; import com.benego.mainactivity; import com.benego.pinnedsectionlistview.pinnedsectionlistadapter; import com.benego.r; import com.benego.entity.localphraseinfo; import com.benego.model.localphrasemodel; public class phraseresultfragment extends basefragment { private string categoryid = null; //private string categoryname = null; public final string source_home = "home"; public final string source_nearby_venue = "nbv"; public final string source_nearby_sub_category = "nbsc"; private localphraseresultadapter adapter = null; private list<localphraseinfo> phrases = null; public string getbundlestring(string key, string defaultvalue) { if (getarguments().containskey(key)) return getarguments().getstring(key); else return defaultvalue; } /** * when creating, retrieve instance's number arguments. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.phraseresult_fragment, container, false); //view v = super.oncreateview(inflater, container, savedinstancestate); searchfragment search_fragment = (searchfragment)getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.phraseresultsearchfragment); search_fragment.resultfragmemt = this; localphrasemodel model = new localphrasemodel(); phrases = null; string frompage = getbundlestring("from", source_home); if (frompage == source_home) { categoryid = getbundlestring("categoryid", "all"); //categoryname = getarguments().getstring("categoryname", "all"); search_fragment.categoryid = categoryid; phrases = model.getphrases(categoryid, null); } else if (frompage == source_nearby_sub_category) { string fssubcategoryid = getbundlestring("subcategoryid", "all"); search_fragment.subcategoryid = fssubcategoryid; phrases = model.getphrasesbysubcategoryid(fssubcategoryid, null); } else if (frompage == source_nearby_venue) { string[] subcategoryids = getarguments().getstringarray("subcategoryids"); search_fragment.subcategoryids = subcategoryids; phrases = model.getphrasesbysubcategoryids(subcategoryids, null); } listview listview = (listview) v.findviewbyid(android.r.id.list); adapter = new localphraseresultadapter(getactivity().getapplicationcontext(), phrases); listview.setadapter(adapter); return v; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public void ondestroyview() { super.ondestroyview(); try { fragment fragment = getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.phraseresultsearchfragment); fragmenttransaction ft = getactivity().getsupportfragmentmanager().begintransaction(); ft.remove(fragment); ft.commit(); } catch (exception e) { } } public void playaudio(int phraseid){ //set mediaplayer try { localphrasemodel model = new localphrasemodel(); byte[] audio = model.getaudio(phraseid); if (audio != null) { file tempmp3 = file.createtempfile(string.valueof(phraseid), ".mp3", getactivity().getcachedir()); tempmp3.deleteonexit(); fileoutputstream fos = new fileoutputstream(tempmp3); fos.write(audio); fos.close(); final fileinputstream fs = new fileinputstream(tempmp3); filedescriptor fd = fs.getfd(); mediaplayer mp = new mediaplayer(); mp.setdatasource(fd); mp.prepare(); mp.start(); mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { try { fs.close(); } catch (ioexception e) { e.printstacktrace(); } } }); } } catch (exception e) { e.printstacktrace(); } } public void refreshview(list<localphraseinfo> results) { this.adapter.setlistdata(results); } public class localphraseresultadapter extends baseadapter implements pinnedsectionlistadapter { private context mcontext; private list<localphraseinfo> _results = null; public localphraseresultadapter(context c, list<localphraseinfo> phrases) { mcontext = c; this.setresults(phrases); } public int getcount() { int size = this._results.size(); return size; } public object getitem(int position) { return _results.get(position); } public long getitemid(int position) { return position; } private void setlistdata( list<localphraseinfo> newlistdata ) { this.setresults(newlistdata); notifydatasetchanged(); }//met public view getview(int position, view convertview, viewgroup parent) { view cview; if (convertview == null) { // if it's not recycled, initialize // attributes cview = layoutinflater.from(mcontext).inflate( r.layout.phraseresult_item, null); } else { cview = convertview; } localphraseinfo pinfo = _results.get(position); cview.settag(pinfo); if (pinfo.getphraseid() > 0) { cview.findviewbyid(r.id.phraseresult_row).setvisibility(view.visible); cview.findviewbyid(r.id.phraseresult_section).setvisibility(view.gone); cview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { localphraseinfo pinfo = (localphraseinfo)v.gettag(); playaudio(pinfo.getphraseid()); } }); textview txtnativeview = (textview) cview.findviewbyid(r.id.nativephraseview); textview txttranslationview = (textview) cview.findviewbyid(r.id.translationphraseview); txtnativeview.settext(pinfo.getnativephrase()); txttranslationview.settext(pinfo.gettranslationphrase()); imageview sv = (imageview)cview.findviewbyid(r.id.phrasesoundicon); if (pinfo.getissoundclipexists()) sv.setvisibility(view.visible); imagebutton detailbtn = (imagebutton)cview.findviewbyid(r.id.btnphrasedetail); if (pinfo.gethasrelatedphrases() || pinfo.getisrootphrase()) { detailbtn.setvisibility(view.visible); detailbtn.settag(pinfo); detailbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { localphraseinfo pinfo = (localphraseinfo)v.gettag(); bundle args = new bundle(); args.putstring("from", "home"); args.putserializable("phrase", pinfo); mainactivity mainactivity = (mainactivity)getactivity(); mainactivity.addfragment(mainactivity.local_tab, phrasedetailfragment.class, args); } }); } } else { cview.findviewbyid(r.id.phraseresult_row).setvisibility(view.gone); cview.findviewbyid(r.id.phraseresult_section).setvisibility(view.visible); textview txtsectionview = (textview)cview.findviewbyid(r.id.phraseresult_section_text); txtsectionview.settext(pinfo.getsubcategoryname()); } return cview; } @override public int getviewtypecount() { return 2; } @override public int getitemviewtype(int position) { localphraseinfo info = (localphraseinfo)getitem(position); if (info.getphraseid() > 0) return 2; else return 1; } @override public boolean isitemviewtypepinned(int viewtype) { return viewtype == 1; } private void setresults(list<localphraseinfo> results) { list<localphraseinfo> sectioned = new arraylist<localphraseinfo>(); string key = null; iterator<localphraseinfo> localiterator = results.iterator(); while (localiterator.hasnext()) { localphraseinfo info = localiterator.next(); if (key == null || !key.endswith(info.getsubcategoryname())) { key = info.getsubcategoryname(); sectioned.add(this.getsectionphraseinfo(info)); } sectioned.add(info); } this._results = sectioned; } private localphraseinfo getsectionphraseinfo(localphraseinfo phrase) { localphraseinfo info = new localphraseinfo(); info.setsubcategoryid(phrase.getsubcategoryid()); info.setsubcategoryname(phrase.getsubcategoryname()); return info; } } }
please me. struggling last 2 days.
if read closely getitemviewtype
method documentation, says :
integers must in range 0 getviewtypecount() - 1
in case, in range [0, 1]
Comments
Post a Comment