您可能应该研究范围和成员变量。问题是,您不能在一个方法中声明一件事,然后尝试从另一方法访问它。
因此,我们将该事物声明为成员变量,并在一个方法中对其进行定义,然后可以在另一方法中使用它。
这样(我的评论已标记为
**):
public class Register extends Activity { // "private" means it can only be accessed from this class private String myLat = null; // ** Declare myLat private String myLon = null; // ** Declare myLon @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); final EditText email_id = (EditText) findViewById(R.id.email_id) ; final EditText name = (EditText) findViewById(R.id.name); final EditText password = (EditText) findViewById(R.id.password); Button button = (Button) findViewById(R.id.button1) ; //generate GCM id GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, "12356"); } else { String TAG = null; Log.v(TAG, regId); } //generate GCM id ended StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); //get current location LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); LocationListener listner = new LocationListener() { @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { double myLonDouble = location.getLongitude(); myLon = Double.toString(myLonDouble); // ** Define myLon double myLatDouble = location.getLatitude(); myLat = Double.toString(myLatDouble); // ** Define myLat } }; manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner); //end get current location button.setonClickListener(new View.onClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // ** Check if they have been defined if (myLat == null || myLon == null) return; //postData(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://xyz.com/folder/register.php"); try { // Add your data List<NamevaluePair> namevaluePairs = new ArrayList<NamevaluePair>(2); namevaluePairs.add(new BasicNamevaluePair("email", email_id.getText().toString())); namevaluePairs.add(new BasicNamevaluePair("name", name.getText().toString())); namevaluePairs.add(new BasicNamevaluePair("password", password.getText().toString())); namevaluePairs.add(new BasicNamevaluePair("regid", regId)); namevaluePairs.add(new BasicNamevaluePair("uid", "2")); namevaluePairs.add(new BasicNamevaluePair("lat",myLat )); namevaluePairs.add(new BasicNamevaluePair("lon",myLon)); httppost.setEntity(new UrlEnpredFormEntity(namevaluePairs)); // Execute HTTP Post Request //Toast.makeText(this, resId, duration) HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } }); }}


