Java 如何将信息传递给方法外部的字符串?

Java 如何将信息传递给方法外部的字符串?,java,android,Java,Android,我是Java语言的新手,有点卡住了,我正在尝试将一个位置传递给字符串Bob。我需要将字符串从onLocationChanged方法传递到字符串url,该url位于该方法之外。我已经创建了一个全局变量,但是字符串bob不包含任何数据 任何帮助都将不胜感激 public class MainActivity extends AppCompatActivity { String bob = ""; LocationManager locationManager; LocationListener

我是Java语言的新手,有点卡住了,我正在尝试将一个位置传递给
字符串Bob
。我需要将
字符串
onLocationChanged
方法传递到
字符串url
,该url位于该方法之外。我已经创建了一个
全局变量
,但是
字符串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";
public类MainActivity扩展了AppCompatActivity{
字符串bob=“”;
地点经理地点经理;
LocationListener LocationListener;
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.GPS_PROVIDER,0,0,locationListener);}
}
}
文本视图和文本视图;
文本视图日期文本视图;
TextView天气预报TextView;
TextView cityTextView;
ImageView天气图像视图;
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestExtView=(TextView)findViewById(R.id.TestExtView);
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());
//位置
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
//Log.i(“Location”,Location.toString());
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
试一试{
List listAddresses=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);
if(listAddresses!=null&&listAddresses.size()>0){
//Log.i(“PlaceInfo”,listAddresses.get(0.toString());
if(listAddresses.get(0.GetLocation()!=null)
{
bob+=listAddresses.get(0.GetLocation()++);
}
Log.i(“你好,bob.toString());
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
};
字符串url=”http://api.openweathermap.org/data/2.5/weather?q=“+bob+”&units=metric&appid=xxxx”;

onLocationChanged
在计算
字符串url
后进行评估

一旦您了解了多线程,您就会明白这一点

目前,如果将
stringurl
移动到
onCreate
的最顶端,代码也不会有什么不同

如果希望正确分配URL,请将其移动到
Log.i(“hello”
)之后,甚至检查日志以确保其正确

就个人而言,我不建议将Android作为学习Java的平台

我不熟悉Java语言

我现在给你的最好的建议是,在努力学好Android之前,停下来,放下Android,去学Java(或者见鬼,从Kotlin开始学习)。Android已经够复杂的了,不需要学习语言

无论如何,关于你的实际问题:

我正在尝试将位置传递给字符串“Bob”。我需要将字符串从“onLocationChanged”方法传递给字符串“url”,该字符串位于该方法之外……我创建了一个“全局”变量,但字符串“Bob”不包含任何数据

您的问题在于
onLocationChanged
方法是一个回调函数,在您尝试设置URL变量的时间点之前(假设您实际注册了回调函数,而您的代码没有显示该回调函数),它实际上不会被调用

换句话说,您在系统中提供了一个钩子,上面写着“当您拥有位置时,请告诉我”,同时您的代码将继续。当系统让您知道位置已返回(您的回调已被调用)时,您可以使用新值来处理它(在您的情况下,我假设,发出网络请求)

因此,您可以将逻辑移到更新
bob
的行后面

希望有帮助