java - app crashes when intent passes variable to another activity -
i making app tracks user , tracks user in theory animal. app goes this, register username , pass when done user can log map reentering correct username , password. issues begin. upon creation of screen map loads users current location , auto sends sms "animals" phone request gps details, sends 2 sms messages, 1 containing gps information. have smsreceiver class reads information , extracts longitude , latitude data, converts double passes map activity converted lnglat variable , displayed on google map marker. issue having can take several minutes sms return gps information, when done , intent used send coordinates map page button must clicked longitude , latitude combined animalcoordinate , marker shown, because og time gap imposible press button @ same time sms retrieved , causes crash data being sent smsreceiver class nothing on other side, , if take intent out of onclick method same thing happens in reverse, map runs intent informaion not there yet , crashes. appreciated has been nightmare.
i sorry if overcomplicated explanation, wanted ake sure explained best could. code below 2 classes.
map class public class mainscreen extends fragmentactivity implements locationlistener { private googlemap map; private locationmanager locationmanager; private string provider; final context context = this; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_screen); map = ((supportmapfragment)getsupportfragmentmanager(). findfragmentbyid(r.id.map)).getmap(); locationmanager service = (locationmanager) getsystemservice(location_service); boolean enabledgps = service .isproviderenabled(locationmanager.gps_provider); boolean enabledwifi = service .isproviderenabled(locationmanager.network_provider); // check if enabled , if not send user gsp settings if (!enabledgps) { toast.maketext(this, "gps signal not found", toast.length_long).show(); intent intent = new intent(settings.action_location_source_settings); startactivity(intent); } locationmanager = (locationmanager) getsystemservice(context.location_service); // define criteria how select locatioin provider -> use // default criteria criteria = new criteria(); provider = locationmanager.getbestprovider(criteria, false); location location = locationmanager.getlastknownlocation(provider); // initialize location fields if (location != null) { toast.maketext(this, "selected provider " + provider, toast.length_short).show(); onlocationchanged(location); } else { //do } // sets map type "hybrid" map.setmaptype(googlemap.map_type_hybrid); bundle b = getintent().getextras(); double lat = location.getlatitude(); double lng = location.getlongitude(); toast.maketext(this, "location " + lat+","+lng, toast.length_long).show(); latlng usercoordinate = new latlng(lat, lng); marker user = map.addmarker(new markeroptions() .position(usercoordinate) .title("you here") .icon(bitmapdescriptorfactory.fromresource(r.drawable.ic_launcher))); //move camera instantly user zoom of 15. map.movecamera(cameraupdatefactory.newlatlngzoom(usercoordinate, 15)); // zoom in, animating camera. map.animatecamera(cameraupdatefactory.zoomto(18), 2000, null); //sends sms 'animal phone' string phoneno = "***********"; string sms = "gpslocation"; try { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(phoneno, null, sms, null, null); toast.maketext(getapplicationcontext(), "sms sent!", toast.length_long).show(); } catch (exception e) { toast.maketext(getapplicationcontext(), "sms faild, please try again later!", toast.length_long).show(); e.printstacktrace(); } } public void map_help(view view) { //method button alertdialog.builder alertdialogbuilder = new alertdialog.builder( context); // set title alertdialogbuilder.settitle("help"); // set dialog message alertdialogbuilder .setmessage("click 'pet' button display pets location." + "this can take few minutes retrieve.") .setcancelable(false) .setpositivebutton("ok",new dialoginterface.onclicklistener(){ public void onclick(dialoginterface dialog,int id) { // if button clicked, close // current activity dialog.cancel(); } }); // create alert dialog alertdialog alertdialog = alertdialogbuilder.create(); // show alertdialog.show(); }; public void find_pet(view view) { //string phoneno = "07516909014"; // string sms = "gpslocation"; // try { // smsmanager smsmanager = smsmanager.getdefault(); //smsmanager.sendtextmessage(phoneno, null, sms, null, null); //toast.maketext(getapplicationcontext(), "sms sent!", // toast.length_long).show(); // } catch (exception e) { // toast.maketext(getapplicationcontext(), // "sms faild, please try again later!", //toast.length_long).show(); // e.printstacktrace(); //} } public void show_pet(view view) { //gets coordinates smsreceiver bundle b = getintent().getextras(); double animallat = b.getdouble("key"); bundle d = getintent().getextras(); double animallon = d.getdouble("key1"); latlng animalcoordinate = new latlng(animallat, animallon); //adds pets marker on map marker animal = map.addmarker(new markeroptions() .position(animalcoordinate) .title("your pet here") .icon(bitmapdescriptorfactory.fromresource(r.drawable.ic_launcher))); } /* request updates @ startup */ @override protected void onresume() { super.onresume(); locationmanager.requestlocationupdates(provider, 400, 1, this); } /* remove locationlistener updates when activity paused */ @override protected void onpause() { super.onpause(); locationmanager.removeupdates(this); } @override public void onlocationchanged(location location) { } @override public void onproviderdisabled(string provider) { toast.maketext(this, "enabled new provider " + provider, toast.length_short).show(); } @override public void onproviderenabled(string provider) { toast.maketext(this, "disabled provider " + provider, toast.length_short).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
smsreceiver class
public class smsreceiver extends broadcastreceiver { string lat = null; string lon = null; string message = null; final smsreceiver context = this; @override public void onreceive(context context, intent intent) { //---get sms message passed in--- bundle bundle = intent.getextras(); smsmessage[] msgs = null; string str = ""; if (bundle != null) { //---retrieve sms message received--- object[] pdus = (object[]) bundle.get("pdus"); msgs = new smsmessage[pdus.length]; (int i=0; i<msgs.length; i++){ msgs[i] = smsmessage.createfrompdu((byte[])pdus[i]); str += msgs[i].getmessagebody().tostring(); }} message = str.tostring(); string[] test = message.split(""); char[] test2 = test[1].tochararray(); //if first character of sms c read gps information if (test2[0] == 'c' || test2[0] =='c') { lat = message.substring(45, 56); lon = message.substring(67, 78); double animallat=double.parsedouble(lat); double animallon=double.parsedouble(lon); //pass coordinates mainscreen intent = new intent(getapplicationcontext(), mainscreen.class); bundle b = new bundle(); b.putdouble("key", animallat); a.putextras(b); startactivity(a); intent c = new intent(getapplicationcontext(), mainscreen.class); bundle d = new bundle(); d.putdouble("key1", animallon); c.putextras(d); startactivity(c); }else { } } private void startactivity(intent a) { // todo auto-generated method stub } private context getapplicationcontext() { // todo auto-generated method stub return null; }}
i want apologize layout of code, first time have pasted code on site. again.
to honest, i'm not sure button
talking. 1 saw in alertdialog
unless missed something. anyway, can disable button
until whatever data has value or can nothing in onclick()
if null
//inside button if (data != null) { ...do stuff in here }
if need more clarification please indicate in code data talking , button
think idea.
Comments
Post a Comment