栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过其他方法访问变量

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过其他方法访问变量

您可能应该研究范围和成员变量。问题是,您不能在一个方法中声明一件事,然后尝试从另一方法访问它。

因此,我们将该事物声明为成员变量,并在一个方法中对其进行定义,然后可以在另一方法中使用它。

这样(我的评论已标记为

**
):

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号