2017-08-25 76 views
0

的外空与谷歌的FusedLocationClientOnsuccesListener匿名内部类存储userLocalityuserCountry两个字符串。字符串将成为我检索<code>LastKnowlocation</code>匿名内部类

在下面的代码:文本集是在locationProvidedTextView正确的(因此userLocalityuserCountry得到一些值),但是当我尝试打印内部类以外的字符串值,它们在某种程度上都成为null

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

代码片段:

// 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); 
      .... 
    } 
+1

事实上,在听众有机会接听电话之前,您似乎正在打印这些值,即使您先声明了它。 – Berger

+0

'getLastLocation()'似乎是一个异步调用。添加几个断点,你会看到如何工作 – Pelocho

+0

'OnSuccess(Location location)'将被异步调用。你的值是'null',因为在值被分配到'onSuccess()'方法之前,这些值将是空的。 – Wizard

回答

1

获得位置后,可能会正在添加需要一些时间。根据您的代码,您正在立即打印mFusedLocationClient.getLastLocation().addOnSuccessListener

您的侦听器不会立即被调用。所以你不会在这些字符串中获得任何价值。

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

1

的问题是,你没有构建任何机制来检索值。换句话说,你的onCreate貌似现在这种权利:

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

值越来越监听被调用后成立,但在这一点上,你已经与onCreate方法来完成。

+0

谢谢。解释是正确的,但它没有提供任何解决方案 –

1

这是正常现象。

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

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

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

为了测试这一点,你可以把: 的addOnSuccessListener一个内部:

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

您将看到消息在

System.out.println("ADDRESS1 ======= " + userLocality); 
0
  @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准确cordinates但是当GPS是禁用或网络不强它可以给错误的读数可能导致在获得空字符串变量在这里你应该尝试改变提供商和网络或GPS提供商的准确性,这可能有所帮助。