java - reinitialize arraylists between activities error -
i have 2 activities loginactivity
, mymapactivity
. problem when try open mymapactivity
(login->mymapactivity (first time activity works)-> logout -> login) second time following error:
08-10 06:55:27.663: e/androidruntime(3630): fatal exception: main 08-10 06:55:27.663: e/androidruntime(3630): java.lang.runtimeexception: unable start activity componentinfo{com.example.pac/com.example.pac.mymapactivity}: java.lang.indexoutofboundsexception: invalid index 0, size 0 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread.performlaunchactivity(activitythread.java:2247) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2297) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread.access$700(activitythread.java:152) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread$h.handlemessage(activitythread.java:1282) 08-10 06:55:27.663: e/androidruntime(3630): @ android.os.handler.dispatchmessage(handler.java:99) 08-10 06:55:27.663: e/androidruntime(3630): @ android.os.looper.loop(looper.java:137) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread.main(activitythread.java:5328) 08-10 06:55:27.663: e/androidruntime(3630): @ java.lang.reflect.method.invokenative(native method) 08-10 06:55:27.663: e/androidruntime(3630): @ java.lang.reflect.method.invoke(method.java:511) 08-10 06:55:27.663: e/androidruntime(3630): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 08-10 06:55:27.663: e/androidruntime(3630): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 08-10 06:55:27.663: e/androidruntime(3630): @ dalvik.system.nativestart.main(native method) 08-10 06:55:27.663: e/androidruntime(3630): caused by: java.lang.indexoutofboundsexception: invalid index 0, size 0 08-10 06:55:27.663: e/androidruntime(3630): @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:251) 08-10 06:55:27.663: e/androidruntime(3630): @ java.util.arraylist.get(arraylist.java:304) 08-10 06:55:27.663: e/androidruntime(3630): @ com.example.pac.mymapactivity.oncreate(mymapactivity.java:232) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activity.performcreate(activity.java:5250) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1097) 08-10 06:55:27.663: e/androidruntime(3630): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 08-10 06:55:27.663: e/androidruntime(3630): ... 11 more
line 232 in mymapactivity:
fantasma1 = mymap.addmarker(newmarkeroptions().position(rlevel.get(v)).icon(bitmapdescriptorfactory.fromresource(r.drawable .marker_white)));
mymapactivity class:
public class mymapactivity extends activity implements animationlayout.listener { public final static string tag = "demo"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } userfunctions = new userfunctions(); setcontentview(r.layout.game_screen); deigma = (textview)findviewbyid(r.id.textview1); mlayout = (animationlayout) findviewbyid(r.id.animation_layout); mlayout.setlistener(this); /*permissions gia parse twn sintetagmenwn*/ //telos permission if(userfunctions.isuserloggedin(getapplicationcontext())){ //logout btnlogout = (button) findviewbyid(r.id.logout); btnlogout.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub userfunctions.logoutuser(getapplicationcontext()); intent login = new intent(getapplicationcontext(), loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); // closing dashboard screen finish(); } }); //telos logout /*menu */ //telos /* user location settings */ locationmanager = (locationmanager) getsystemservice(context.location_service); location location = null; // locationmanager.getlastfix().reset(); if(locationmanager.isproviderenabled(locationmanager.gps_provider)||locationmanager.isproviderenabled(locationmanager.network_provider)){ criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setaltituderequired(true); criteria.setspeedrequired(true); criteria.setcostallowed(true); criteria.setbearingrequired(true); //api level 9 , criteria.sethorizontalaccuracy(criteria.accuracy_high); criteria.setverticalaccuracy(criteria.accuracy_high); criteria.setbearingaccuracy(criteria.accuracy_low); criteria.setspeedaccuracy(criteria.accuracy_high); provider = locationmanager.getbestprovider(criteria, true); deigma.settext(string.valueof(locationmanager)); location = locationmanager.getlastknownlocation(provider); locationmanager.requestlocationupdates(provider, 0, 0, locationlistener); /*if (location != null) { log.e("provider scelto", provider); onlocationchanged(location); log.e("latitude", string.valueof("da")); log.e("longitude", string.valueof("no")); }*/ } //telos rithmisewn /* helper function. put in same page, or in library */ protected static string downloadfile(string url) { // fill-in url content stringbuilder builder = new stringbuilder(); // local objects declarations httpclient client = new defaulthttpclient(); httpget httpget = new httpget(url); try { httpresponse response = client.execute(httpget); statusline statusline = response.getstatusline(); int statuscode = statusline.getstatuscode(); if (statuscode == 200) { httpentity entity = response.getentity(); inputstream content = entity.getcontent(); bufferedreader reader = new bufferedreader( new inputstreamreader(content)); string line; while ((line = reader.readline()) != null) { builder.append(line); } } else { // failed download file } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } return builder.tostring(); } /*telos*/ private final locationlistener locationlistener = new locationlistener() { @override public void onlocationchanged(location location) { locationmanager.removeupdates(this); float accuracy = (float) location.getaccuracy(); //deigma.settext(float.tostring(accuracy)); //updatewithnewlocation(location); mylocationmarker.remove(); lat = location.getlatitude(); lng = location.getlongitude(); thesi_xristi=new latlng(lat,lng); mylocationmarker = mymap.addmarker(new markeroptions() .position(thesi_xristi) .icon(bitmapdescriptorfactory.fromresource(r.drawable.k))); for(int vb23=0;vb23<markers_balls.size();vb23++){ double rla = rballs.get(vb23).latitude; double rlo = rballs.get(vb23).longitude; if(calculatedistance(lat, lng, rla, rlo,"m")<0.00080){ markers_balls.get(vb23).remove(); markers_balls.remove(vb23); score=score+10; } } deigma.settext(score +" ac: "+accuracy); } @override public void onproviderdisabled(string provider) { //updatewithnewlocation(null); } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } }; @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.my_map, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.menu_legalnotices: string licenseinfo = googleplayservicesutil.getopensourcesoftwarelicenseinfo( getapplicationcontext()); alertdialog.builder licensedialog = new alertdialog.builder(mymapactivity.this); licensedialog.settitle("legal notices"); licensedialog.setmessage(licenseinfo); licensedialog.show(); return true; } return super.onoptionsitemselected(item); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(getapplicationcontext()); if (resultcode == connectionresult.success){ //toast.maketext(getapplicationcontext(), // "isgoogleplayservicesavailable success", //toast.length_long).show(); }else{ googleplayservicesutil.geterrordialog(resultcode, this, rqs_googleplayservices); } } private double calculatedistance(double lat1, double lon1, double lat2, double lon2, string unit) { double theta = lon1 - lon2; double dist = math.sin(deg2rad(lat1)) * math.sin(deg2rad(lat2)) + math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * math.cos(deg2rad(theta)); dist = math.acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.1515; if (unit == "k") { dist = dist * 1.609344; } else if (unit == "m") { dist = dist * 0.8684; } return (dist); } /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*:: function converts decimal degrees radians :*/ /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ private double deg2rad(double deg) { return (deg * math.pi / 180.0); } /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*:: function converts radians decimal degrees :*/ /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ private double rad2deg(double rad) { return (rad * 180.0 / math.pi); } public void onclickcontentbutton(view v) { mlayout.togglesidebar(); } @override public void onbackpressed() { if (mlayout.isopening()) { mlayout.closesidebar(); } else { finish(); } } /* callback of animationlayout.listener monitor status of sidebar */ @override public void onsidebaropened() { log.d(tag, "opened"); } /* callback of animationlayout.listener monitor status of sidebar */ @override public void onsidebarclosed() { log.d(tag, "opened"); } /* callback of animationlayout.listener monitor status of sidebar */ @override public boolean oncontenttouchedwhenopening() { // content area touched when sidebar opening, close sidebar log.d(tag, "going close sidebar"); mlayout.closesidebar(); return true; } @override public void ondestroy() { super.onpause(); rlevel.clear(); rlevel2.clear(); rlevel3.clear(); rballs.clear(); markers_balls.clear(); go.cancel(); } }
i trying reinitialize arraylists ondestroy
, but, getting same problem.
the error occur caused by: java.lang.indexoutofboundsexception: invalid index 0, size 0
the arraylist re initialize not added value on arraylist. arraylist size zero.
you fetch value @ index of 0.
so throws indeoutofboundsexception.
please check arraylist size before fetch value.
Comments
Post a Comment