我是Java语言的新手并且已经陷入困境,我正试图将位置传递给String Bob
。我需要将string
从onLocationChanged
方法传递到方法外部的String url
。我创建了global variable
,但String bob
不包含任何数据。
非常感谢任何帮助。
public class MainActivity extends AppCompatActivity {
String bob = "";
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);}
}
}
TextView tempTextView;
TextView dateTextView;
TextView weatherDescTextView;
TextView cityTextView;
ImageView weatherImageView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tempTextView = (TextView) findViewById(R.id.tempTextView);
dateTextView = (TextView) findViewById(R.id.dateTextView);
weatherDescTextView = (TextView) findViewById(R.id.weatherDescTextView);
cityTextView = (TextView) findViewById(R.id.cityTextView);
weatherImageView = (ImageView) findViewById(R.id.weatherImageView);
dateTextView.setText(getCurrentDate());
// location
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Log.i("Location", location.toString());
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (listAddresses != null && listAddresses.size() > 0) {
//Log.i("PlaceInfo", listAddresses.get(0).toString());
if (listAddresses.get(0).getLocality() != null)
{
bob += listAddresses.get(0).getLocality() + " ";
}
Log.i("hello", bob.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider ) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
String url = "http://api.openweathermap.org/data/2.5/weather?q=" +bob+ "&units=metric&appid=xxxx";
答案 0 :(得分:0)
onLocationChanged
在>>计算String url
之后进行评估。
一旦了解了多线程,您就会明白这一点。
目前,如果您将String url
移至onCreate
的最顶层,您的代码就会有所不同。
如果您希望正确分配网址,请在Log.i("hello"
之后移动,甚至检查您的日志以确保其正确
就个人而言,我不建议将Android作为学习Java的平台
答案 1 :(得分:0)
我是Java语言的新手
我现在最好的建议是停止,放下Android,然后在尝试学习Android之前学习Java(或者地狱,开始使用Kotlin)。 Android虽然很复杂,但也没有尝试学习语言。
无论如何,关于你的实际问题:
我试图将位置传递给String&#39; Bob&#39;。我需要从&#39; onLocationChanged&#39;传递一个字符串。方法,字符串&#39; url&#39;除了方法之外...我创造了一个全球性的&#39;变量,但String&#39; bob&#39;没有任何数据。
您遇到的问题是,onLocationChanged
方法是回调实际上不会被调用,直到您尝试设置的时间要晚得多URL变量(假设你实际注册回调,你的代码没有显示)。
换句话说,你正在为系统提供一个钩子,说明,&#34;当你有位置时,让我知道&#34;同时你的代码继续。当系统让你知道位置回来时(你的回调被调用),你可以使用新的值来做某事(在你的情况下,我假设,发出网络请求)。
因此,您可以将逻辑移到更新bob
的行之后。
希望有所帮助。