【问题标题】:String becomes null outside of Anonymous Inner Class字符串在匿名内部类之外变为空
【发布时间】:2017-08-25 12:55:52
【问题描述】:

我正在使用谷歌的FusedLocationClient 检索LastKnowlocation,并将userLocalityuserCountry 作为两个字符串存储在OnsuccesListener 匿名内部类中。

在下面的代码中:locationProvidedTextView 中的文本集是正确的(因此 userLocalityuserCountry 得到一些值)但是当我尝试在内部类之外打印字符串值时,它们不知何故都变成了 @ 987654329@。

这可能是一个愚蠢的问题,但是我做错了什么?我需要在另一个 OnclickListener 方法中使用这些值。

代码sn-p:

    // GlOBAL VARIABLES
   ....
   private  String userCountry;
   private  String userLocality;

@Override
protected void onCreate(Bundle savedInstanceState) {
      ....
 mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                        try {
                            List<Address> addresses = geoCoder
        .getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                            if (addresses.size() > 0) {
                                if (addresses.size() > 0) {
                                     userLocality = addresses.get(0).getLocality();
                                     userCountry = addresses.get(0).getCountryName();

                                    locationProvidedTextView.setText("Your address: " + userLocality + ", " + userCountry);

                                }
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }

                    }
                }
            });
 // HERE BOTH STRINGS BECOME NULL
    System.out.println("ADDRESS1 ======= " + userLocality);
    System.out.println("ADDRESS2 ======= " + userCountry);
          ....
   }

【问题讨论】:

  • 事实上,即使您先声明了它,您也似乎在侦听器有机会被调用之前打印了这些值。
  • getLastLocation() 似乎是一个异步调用。添加几个断点,您将看到它是如何工作的
  • OnSuccess(Location location) 将被异步调用。您的值是null,因为在将值分配给onSuccess() 方法之前,这些值将为空。

标签: java android anonymous-inner-class


【解决方案1】:

获取位置需要一些时间。根据您的代码,您将立即打印mFusedLocationClient.getLastLocation().addOnSuccessListener

您的听众不会立即被呼叫。所以你不会在这些字符串中得到任何值。

更好的实现方式是,在另一个类中执行所有与位置相关的事情,而不是在 onCreate 中。然后使用interface 得到结果。

【讨论】:

    【解决方案2】:

    问题是您没有建立任何机制来检索值。换句话说,您的 onCreate 现在看起来像这样:

    onCreate{
    // set listener
    // print stuff out
    }
    

    在调用侦听器后设置值,但此时您已经完成了 onCreate 方法。

    【讨论】:

    • 谢谢。解释是正确的,但它没有提供任何解决方案
    【解决方案3】:

    这是正常的。

    我不知道您的代码,但似乎 addOnSuccessListener 是一种异步机制。

    所以你想在异步部分之外显示尚未设置的值。

    换句话说,您尝试显示一个尚未设置的值。

    要对此进行测试,您可以输入: 在 addOnSuccessListener 里面一个:

    System.out.println("User locality found");
    

    您会看到该消息可能在

    之后出现
    System.out.println("ADDRESS1 ======= " + userLocality);
    

    【讨论】:

      【解决方案4】:
                  @Override
                  public void onSuccess(Location location) {
                      // Got last known location. In some rare situations this can be null.
                      if (location != null) {
                          Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                          try {
                              List<Address> addresses = geoCoder
          .getFromLocation(location.getLatitude(), location.getLongitude(), 1);
      
                              if (addresses.size() > 0) {
                                  if (addresses.size() > 0) {
                                       userLocality = addresses.get(0).getLocality();
                                       userCountry = addresses.get(0).getCountryName();
      
                                      locationProvidedTextView.setText("Your address: " + userLocality + ", " + userCountry);
      
                                  }
                              }
                          } catch (IOException e1) {
                              e1.printStackTrace();
                          }
      

      我猜这个方法每次都将数据设置为 userLocality 和 userCountry 时,它会准确地获取基于 GPS 的坐标,但是当 GPS 被禁用或网络不强时,它可能会给出错误的读数,这可能会导致你在这里得到空字符串变量应该尝试更改提供商以及网络或 gps 提供商的准确性,这可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多