Access Variables From Another Method
i am trying to submit user registration form in android with current location. I am new to android and java development. when i try to access myLat and myLan of the onLocationChang
Solution 1:
You should probably study up on scope and member variables. The thing is, you can't declare one thing in one method then attempt to access it from another method.
So, we declare that thing as a member variable, and define it in one method, then it can be used in another method.
Like so (I've marked my comments with **
):
publicclassRegisterextendsActivity {
// "private" means it can only be accessed from this classprivateStringmyLat=null; // ** Declare myLatprivateStringmyLon=null; // ** Declare myLon@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
finalEditTextemail_id= (EditText) findViewById(R.id.email_id) ;
finalEditTextname= (EditText) findViewById(R.id.name);
finalEditTextpassword= (EditText) findViewById(R.id.password);
Buttonbutton= (Button) findViewById(R.id.button1) ;
//generate GCM id
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
finalStringregId= GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "12356");
} else {
StringTAG=null;
Log.v(TAG, regId);
}
//generate GCM id ended
StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//get current locationLocationManagermanager= (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListenerlistner=newLocationListener() {
@OverridepublicvoidonStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonLocationChanged(Location location) {
doublemyLonDouble= location.getLongitude();
myLon = Double.toString(myLonDouble); // ** Define myLondoublemyLatDouble= location.getLatitude();
myLat = Double.toString(myLatDouble); // ** Define myLat
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner);
//end get current location
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View arg0) {
// TODO Auto-generated method stub// ** Check if they have been definedif (myLat == null || myLon == null)
return;
//postData();HttpClienthttpclient=newDefaultHttpClient();
HttpPosthttppost=newHttpPost("http://xyz.com/folder/register.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>(2);
nameValuePairs.add(newBasicNameValuePair("email", email_id.getText().toString()));
nameValuePairs.add(newBasicNameValuePair("name", name.getText().toString()));
nameValuePairs.add(newBasicNameValuePair("password", password.getText().toString()));
nameValuePairs.add(newBasicNameValuePair("regid", regId));
nameValuePairs.add(newBasicNameValuePair("uid", "2"));
nameValuePairs.add(newBasicNameValuePair("lat",myLat ));
nameValuePairs.add(newBasicNameValuePair("lon",myLon));
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request//Toast.makeText(this, resId, duration)HttpResponseresponse= httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
}
}
Solution 2:
You are having problems with the scope of your variables. The myLatDouble and myLon Double that you are initializing only exist within the onLocationChanged method where they are created. You would have to declare them in a different place so that they do not go out of scope when you try to access them or you would have to pass them into the nameValuePairs code as an argument
Post a Comment for "Access Variables From Another Method"