Wednesday, January 3, 2018

android Global Variable Magically Getting Set To Null

android Global Variable Magically Getting Set To Null




android - Global Variable Magically Getting Set To Null?? -

i using smsreceiver class receives sms , sends sms main activity utilize in map application. in onreceive method toast , value fine, when seek utilize variable later downwards code reverse geocode coordinates address global variable null 1 time again :(

the method called showonmapclicked on line 324 variable null again.

the sms beingness sent ddms.

somebody please help, cant figure out!

onreceive class:

bundle com.example.maps; import android.content.broadcastreceiver; import android.content.context; import android.os.bundle; import android.telephony.smsmessage; import android.widget.toast; import android.content.intent; public class smsreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { //adding bundle extras used main class bundle bundle = intent.getextras(); smsmessage[] msgs = null; string str = "sms "; //adding sms array stored if (bundle != null) { object[] pdus = (object[]) bundle.get("pdus"); msgs = new smsmessage[pdus.length]; (int = 0; < msgs.length; i++) { msgs[i] = smsmessage.createfrompdu((byte[])pdus[i]); if (i == 0) { //appending info string displayed str += msgs[i].getoriginatingaddress(); str += ": "; } str += msgs[i].getmessagebody().tostring(); } toast.maketext(context, str, toast.length_long).show(); //creating new intent intent mainactivityintent = new intent(context, mainactivity.class); mainactivityintent.setflags(intent.flag_activity_new_task); context.startactivity(mainactivityintent); //making intent broadcast main class , putting in displayed intent broadcastintent = new intent(); broadcastintent.setaction("sms_received_action"); //setting name of string "sms" broadcastintent.putextra("sms", str); context.sendbroadcast(broadcastintent); //stops sms going straight inbox this.abortbroadcast(); } } }

main activity class:

bundle com.example.maps; //daniel tromp - assignment 2 - 12002076 //imports days! import java.io.ioexception; import java.util.list; import java.util.locale; import com.example.maps.r; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesclient; import com.google.android.gms.location.locationclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.latlng; import android.app.activity; import android.app.alertdialog; import android.content.broadcastreceiver; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.intentfilter; import android.location.address; import android.location.geocoder; import android.location.location; import android.os.asynctask; import android.os.bundle; import android.telephony.smsmanager; import android.text.inputfilter; import android.text.inputtype; import android.text.method.digitskeylistener; import android.util.log; import android.view.view; import android.widget.edittext; import android.widget.linearlayout; import android.widget.progressbar; import android.widget.textview; import android.widget.toast; //implementing needed services public class mainactivity extends activity implements googleplayservicesclient.connectioncallbacks, googleplayservicesclient.onconnectionfailedlistener, locationlistener { // initializing needed globals location mlocation; string addresssms; string addresstext; public googlemap mmap; private textview maddress; private progressbar mactivityindicator; private locationclient mlocationclient; broadcastreceiver smssentreceiver, smsdeliveredreceiver; intentfilter intentfilter; // milliseconds per sec private static final int milliseconds_per_second = 1000; // update frequency in seconds public static final int update_interval_in_seconds = 5; // update frequency in milliseconds private static final long update_interval = milliseconds_per_second * update_interval_in_seconds; // fastest update frequency, in seconds private static final int fastest_interval_in_seconds = 1; // fast frequency ceiling in milliseconds private static final long fastest_interval = milliseconds_per_second * fastest_interval_in_seconds; // define object holds accuracy , frequency parameters private locationrequest mlocationrequest; private broadcastreceiver intentreceiver = new broadcastreceiver() { @override //on receiving message, textview set sms message public void onreceive(context context, intent intent) { addresssms = intent.getstringextra("sms"); //toasting variable displays sms message toast.maketext(getbasecontext(), addresssms, toast.length_long).show(); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setupmapifneeded(); maddress = (textview) findviewbyid(r.id.address); // app loads, address bar displayed mactivityindicator = (progressbar) findviewbyid(r.id.address_progress); // setting map type hybrid mmap.setmaptype(googlemap.map_type_hybrid); // create locationrequest object mlocationrequest = locationrequest.create(); // utilize high accuracy mlocationrequest.setpriority(locationrequest.priority_high_accuracy); // set update interval 5 seconds mlocationrequest.setinterval(update_interval); // set fastest update interval 1 sec mlocationrequest.setfastestinterval(fastest_interval); /* * create new location client, using enclosing class handle * callbacks. */ mlocationclient = new locationclient(this, this, this); //creating new intent intentfilter = new intentfilter(); intentfilter.addaction("sms_received_action"); registerreceiver(intentreceiver, intentfilter); } private void setupmapifneeded() { // null check confirm have not instantiated // map. if (mmap == null) { mmap = ((mapfragment) getfragmentmanager().findfragmentbyid( r.id.map)).getmap(); // check if successful in obtaining map. if (mmap != null) { // map verified. safe manipulate map. } } } // when application started @override protected void onstart() { super.onstart(); // connect client. mlocationclient.connect(); } private class getaddresstask extends asynctask<location, void, string> { context mcontext; public getaddresstask(context context) { super(); mcontext = context; } protected string doinbackground(location... params) { geocoder geocoder = new geocoder(mcontext, locale.getdefault()); // current location input parameter list location loc = params[0]; // create list contain result address list<address> addresses = null; seek { /* * homecoming 1 address. */ addresses = geocoder.getfromlocation(loc.getlatitude(), loc.getlongitude(), 1); } grab (ioexception e1) { log.e("locationsampleactivity", "io exception in getfromlocation()"); e1.printstacktrace(); homecoming ("io exception trying address"); } grab (illegalargumentexception e2) { // error message post in log string errorstring = "illegal arguments " + double.tostring(loc.getlatitude()) + " , " + double.tostring(loc.getlongitude()) + " passed address service"; log.e("locationsampleactivity", errorstring); e2.printstacktrace(); homecoming errorstring; } // if reverse geocode returned address if (addresses != null && addresses.size() > 0) { // first address address address = addresses.get(0); /* * format first line of address (if available), city, , * country name. */ addresstext = string.format( "%s, %s, %s", // if theres street address, add together address.getmaxaddresslineindex() > 0 ? address .getaddressline(0) : "", // locality city address.getlocality(), // country of address address.getcountryname()); // homecoming text homecoming addresstext; } else { homecoming "no address found"; } } protected void onpostexecute(string address) { // set activity indicator visibility "gone" mactivityindicator.setvisibility(view.gone); // display results of lookup. maddress.settext(address); } } // when connection made live updates public void onconnected(bundle databundle) { // display connection status toast.maketext(this, "connected", toast.length_short).show(); // start periodic updates mlocationclient.requestlocationupdates(mlocationrequest, this); } // when new coordinates set ddms, map alter // location , new address public void onlocationchanged(location location) { // study ui location updated latlng latlng = new latlng(location.getlatitude(), location.getlongitude()); mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); mactivityindicator.setvisibility(view.visible); /* * reverse geocoding long-running , synchronous. run on * background thread. pass current location background task. * when task finishes, onpostexecute() displays address. */ (new getaddresstask(this)).execute(location); } @override public void onconnectionfailed(connectionresult arg0) { } @override public void ondisconnected() { } // click on button read me file know how utilize // application public void readmeclicked(view v) { // create dialog pop , explain application user alertdialog alertdialog = new alertdialog.builder(this).create(); // read // update alertdialog.settitle("read me"); alertdialog .setmessage("my app created able send out emergency sms individual contain emergency message current location. using ddms, coordinates can input , app go specific location , pull right address of location"); // making dialog show alertdialog.show(); } public void getfromlocationclicked(view v) { alertdialog.builder alertdialog = new alertdialog.builder( mainactivity.this); // setting dialog title alertdialog.settitle("emergency sms"); // setting dialog message alertdialog.setmessage("enter emergency contact number"); final edittext input = new edittext(mainactivity.this); linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); input.setlayoutparams(lp); alertdialog.setview(input); // setting positive "continue" button alertdialog.setpositivebutton("continue", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { string testing = input.gettext().tostring(); getfromlocation(testing); } }); // setting negative "cancel" button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); }; public void getfromlocation(string straddress) { geocoder coder = new geocoder(this); list<address> address; seek { address = coder.getfromlocationname(straddress, 5); if (address != null) { address location = address.get(0); double reclat = location.getlatitude(); double reclng = location.getlongitude(); latlng p = new latlng(reclat, reclng); toast.maketext(getbasecontext(), p + "", toast.length_long) .show(); mmap.animatecamera(cameraupdatefactory.newlatlngzoom(p, 16)); } } grab (ioexception ie) { ie.printstacktrace(); } } //showing value when show button clicked public void showonmapclicked(view v) { //when toasts, addresssms value null!?? toast.maketext(getbasecontext(), addresssms, toast.length_long).show(); //getfromlocation(addresssms); } public void showtoast(string message) { string test = message; toast.maketext(getbasecontext(), test, toast.length_long).show(); } // click button start generate emergency sms public void sendaddressclicked(final view v) { alertdialog.builder alertdialog = new alertdialog.builder( mainactivity.this); // setting dialog title alertdialog.settitle("emergency sms"); // setting dialog message alertdialog.setmessage("enter emergency contact number"); final edittext input = new edittext(mainactivity.this); linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); input.setlayoutparams(lp); alertdialog.setview(input); input.setfilters(new inputfilter[] { // maximum 2 characters. new inputfilter.lengthfilter(10), // digits only. digitskeylistener.getinstance(), // not strictly needed, imho. }); // setting positive "continue" button alertdialog.setpositivebutton("continue", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // setting sms message , adding current // location gathered map. string textmessage = input.gettext().tostring(); // checking if phone number not blank if (textmessage.isempty()) { // if phone number not entered toast.maketext(getbasecontext(), "cannot blank. come in phone number.", toast.length_long).show(); sendaddressclicked(v); } else { entermessage(textmessage); } } }); // setting negative "cancel" button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); }; // method send emergency method , current location private void sendsms(string phonenumber, string message) { // sending sms emulator number input smsmanager sms = smsmanager.getdefault(); sms.sendtextmessage(phonenumber, null, message, null, null); } // 1 time user has input number, message asked public void entermessage(final string number) { // creating dialog input emergency message alertdialog.builder alertdialog = new alertdialog.builder( mainactivity.this); // setting dialog title alertdialog.settitle("emergency sms"); // setting dialog message alertdialog.setmessage("enter emergency sms"); final edittext input = new edittext(mainactivity.this); linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); input.setlayoutparams(lp); alertdialog.setview(input); input.setinputtype(inputtype.type_class_text); // setting positive "send sms" button alertdialog.setpositivebutton("send sms", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // write code here execute after dialog // setting sms message , adding current // location gathered map. string textmessage = input.gettext().tostring(); string sms = textmessage + " " + "my current address is: " + addresstext; string phonenumber = number; // checking see if sms blank if (textmessage.isempty()) { toast.maketext(getbasecontext(), "cannot blank. come in emergency sms.", toast.length_long).show(); entermessage(phonenumber); } else { // sending variables through sendsms // method sent sendsms(phonenumber, sms); } } }); // setting negative "cancel" button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // write code here execute after dialog dialog.cancel(); } }); // showing alert message alertdialog.show(); }; }

try overriding onsaveinstancestate(bundle bundle) method in mainactivity , utilize logs see if getting called before info getting wiped out. suspect youll see getting called. if so, save globals in bundle , retrieve them overriding onrestoreinstancestate(bundle bundle)

the doc should help how save , restore. whenever activity needs killed memory or orientation change, oncreate called , globals can lost way.

this might help more details.

android sms google-maps-android-api-2

go to link download
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.