<legend id='SKfik'><style id='SKfik'><dir id='SKfik'><q id='SKfik'></q></dir></style></legend>
  • <small id='SKfik'></small><noframes id='SKfik'>

    1. <tfoot id='SKfik'></tfoot>
        <bdo id='SKfik'></bdo><ul id='SKfik'></ul>

        <i id='SKfik'><tr id='SKfik'><dt id='SKfik'><q id='SKfik'><span id='SKfik'><b id='SKfik'><form id='SKfik'><ins id='SKfik'></ins><ul id='SKfik'></ul><sub id='SKfik'></sub></form><legend id='SKfik'></legend><bdo id='SKfik'><pre id='SKfik'><center id='SKfik'></center></pre></bdo></b><th id='SKfik'></th></span></q></dt></tr></i><div id='SKfik'><tfoot id='SKfik'></tfoot><dl id='SKfik'><fieldset id='SKfik'></fieldset></dl></div>

        从其他方法访问变量

        时间:2023-09-30
        <tfoot id='Rf5O3'></tfoot>

        <small id='Rf5O3'></small><noframes id='Rf5O3'>

          <i id='Rf5O3'><tr id='Rf5O3'><dt id='Rf5O3'><q id='Rf5O3'><span id='Rf5O3'><b id='Rf5O3'><form id='Rf5O3'><ins id='Rf5O3'></ins><ul id='Rf5O3'></ul><sub id='Rf5O3'></sub></form><legend id='Rf5O3'></legend><bdo id='Rf5O3'><pre id='Rf5O3'><center id='Rf5O3'></center></pre></bdo></b><th id='Rf5O3'></th></span></q></dt></tr></i><div id='Rf5O3'><tfoot id='Rf5O3'></tfoot><dl id='Rf5O3'><fieldset id='Rf5O3'></fieldset></dl></div>

              • <bdo id='Rf5O3'></bdo><ul id='Rf5O3'></ul>
              • <legend id='Rf5O3'><style id='Rf5O3'><dir id='Rf5O3'><q id='Rf5O3'></q></dir></style></legend>
                  <tbody id='Rf5O3'></tbody>
                  本文介绍了从其他方法访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用当前位置在 android 中提交用户注册表单.我是 android 和 java 开发的新手.当我尝试在我的名称值对代码中访问 onLocationChanged 方法的 myLat 和 myLan 时,它无法找到这两个变量.如何访问我的名称值对代码中的两个变量.

                  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 onLocationChanged method in my name value pairs code , it is unable to find both the variables. How can i access both the variables in my name value pair code.

                  package com.imran;
                  
                  import java.io.IOException;
                  import java.util.ArrayList;
                  import java.util.List;
                  
                  import org.apache.http.HttpResponse;
                  import org.apache.http.NameValuePair;
                  import org.apache.http.client.ClientProtocolException;
                  import org.apache.http.client.HttpClient;
                  import org.apache.http.client.entity.UrlEncodedFormEntity;
                  import org.apache.http.client.methods.HttpPost;
                  import org.apache.http.impl.client.DefaultHttpClient;
                  import org.apache.http.message.BasicNameValuePair;
                  
                  import com.google.android.gcm.GCMRegistrar;
                  import com.google.android.maps.MyLocationOverlay;
                  
                  import android.location.Location;
                  import android.location.LocationListener;
                  import android.location.LocationManager;
                  import android.os.Bundle;
                  import android.os.StrictMode;
                  import android.util.Log;
                  import android.view.View;
                  import android.widget.Button;
                  import android.widget.EditText;
                  import android.widget.Toast;
                  import android.app.Activity;
                  import android.content.Context;
                  
                  public class Register extends Activity {
                  
                  
                  
                  
                      @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();
                              final String myLon = Double.toString(myLonDouble);
                                  double myLatDouble = location.getLatitude();
                                  final String myLat = Double.toString(myLatDouble);
                  
                              }
                          };
                          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
                          //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 UrlEncodedFormEntity(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
                              }
                  
                      }
                  });
                  
                      }
                  
                  
                  }
                  

                  推荐答案

                  你应该好好学习一下 范围和成员变量.问题是,您不能在一个方法中声明一件事,然后尝试从另一个方法访问它.

                  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 **):

                  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 UrlEncodedFormEntity(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
                              }
                  
                          }
                      });
                  
                      }
                  
                  
                  }
                  

                  这篇关于从其他方法访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Servlet:提交响应后无法转发 下一篇:在圆中寻找坐标

                  相关文章

                  最新文章

                  1. <legend id='7H7Qi'><style id='7H7Qi'><dir id='7H7Qi'><q id='7H7Qi'></q></dir></style></legend>

                      <bdo id='7H7Qi'></bdo><ul id='7H7Qi'></ul>
                    <i id='7H7Qi'><tr id='7H7Qi'><dt id='7H7Qi'><q id='7H7Qi'><span id='7H7Qi'><b id='7H7Qi'><form id='7H7Qi'><ins id='7H7Qi'></ins><ul id='7H7Qi'></ul><sub id='7H7Qi'></sub></form><legend id='7H7Qi'></legend><bdo id='7H7Qi'><pre id='7H7Qi'><center id='7H7Qi'></center></pre></bdo></b><th id='7H7Qi'></th></span></q></dt></tr></i><div id='7H7Qi'><tfoot id='7H7Qi'></tfoot><dl id='7H7Qi'><fieldset id='7H7Qi'></fieldset></dl></div>
                  2. <small id='7H7Qi'></small><noframes id='7H7Qi'>

                    <tfoot id='7H7Qi'></tfoot>