Android 变量作用域;或者,我的绳子到底在哪里
我正在开发一个简单的 Android 应用程序,带有 GPS 监听器和网络视图。
我可以毫无问题地获取纬度和经度。问题是,我想将纬度和经度放入 URL 中(例如 myurl.com/mypage.php?lat=57&lon=21)...但是存储数据的变量仅限于其类。我不知道如何声明或创建可以在整个主类中使用的变量。这是我的代码:
public class WTest2Activity extends Activity {
public String txt;
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {} /* do nothing */
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
}
}
I'm working on a simple Android app, with a GPS listener and a webview.
I can get the latitude and longitude with no issue. The problem is, I want to put the latitude and longitude into a URL (like myurl.com/mypage.php?lat=57&lon=21)... but the variable the data is stored in is confined to its class. I can't figure out how to declare or create a variable that I can use throughout the entire main class. Here's my code:
public class WTest2Activity extends Activity {
public String txt;
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {} /* do nothing */
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
OnCreate
方法是在 Activity 启动时调用的方法。此时,txt
为null
。这就是为什么它不会显示在您的网址中。然后,onLocationChanged
设置了txt
的值,但是之后您在哪里使用它呢? 无处!您应该将
onCreate
中的操作移至onLocationChanged
:Your
OnCreate
method is the method called at start of Activity. At that time,txt
isnull
. That's why it doens't show in your url. Then,onLocationChanged
sets the value oftxt
but where are you using this afterwards? nowhere!.you should move what you do in
onCreate
toonLocationChanged
:此处添加新行:
然后,在您的
public void onLocationChanged(Location loc)
中,尝试设置location.setLatitude(loc.getLatitide);
和location.setLongitude (loc.getLongitude);
然后,您可以使用全局变量
location
在任何地方访问您的位置add a new line here:
then, in your
public void onLocationChanged(Location loc)
, try settinglocation.setLatitude(loc.getLatitide);
andlocation.setLongitude(loc.getLongitude);
then, you can access your location anywhere with your global variable
location
兄弟,这只是你代码中的一个小逻辑错误。让我解释一下。您在加载 URL 后立即请求位置更新。现在位置监听是不同的线程,它正在更新 txt 变量的值。并且更新位置需要时间。而且每次花费的时间可能不同。这就是为什么您需要将 loadUrl 代码移至 onLocationChanged 方法。
Buddy its just small logical mistake in your code. Let me explain. You are requesting location updates then right after that you are loading URL. Now location listening is different thread which is updating value of txt variable. And updating location takes time. And taken time can be different every time. That is why you need to move your loadUrl code to onLocationChanged method.