访问内部类变量到另一个方法

时间:2019-12-05 18:20:00

标签: java android

请帮助。我不是普通的Java作家,所以我无法解决此问题: getLastLocation()方法中的onCreate返回LatLang的0.0,而使用getLastLocation()方法本身的值是正确的。另外,在Android Studio的getLastLocation中,从未使用过Lat和Lang参数。

请帮助我纠正这个难题。

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = new Bundle();
    bundle.putDouble  ("loclat", 25.4358);
    bundle.putDouble("loclang", 81.8463);
    Fragment SunFragment = new SunFragment();
    SunFragment.setArguments(bundle);
    setContentView(R.layout.activity_main);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    getLastLocation(Lat, Long);
    //Value is 0 here
    Toast.makeText(getApplicationContext(),Double.toString(Lat), Toast.LENGTH_LONG).show();  
      SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,
          getSupportFragmentManager(), Lat, Long);
      ViewPager viewPager = findViewById(R.id.view_pager);
      viewPager.setAdapter(sectionsPagerAdapter);
      TabLayout tabs = findViewById(R.id.tabs);
      tabs.setupWithViewPager(viewPager);

  }
  @SuppressLint("MissingPermission")
  public void getLastLocation(double Lat, double Long){
    if (checkPermissions()) {
      if (isLocationEnabled()) {
        mFusedLocationClient.getLastLocation().addOnCompleteListener(
            new OnCompleteListener<Location>() {
              @Override
              public void onComplete(@NonNull Task<Location> task) {
                Location location = task.getResult();
                if (location == null) {
                  requestNewLocationData();
                } else {
                 final double Lat = location.getLatitude();
                 final double Long = location.getLongitude();
                 // Giving the value here
                 Toast.makeText(getApplicationContext(),"Long"+ Long, Toast.LENGTH_LONG).show();   
                }
              }
            }
        );
      } else {
        Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
      }
    } else {
      requestPermissions();
    }
  }

2 个答案:

答案 0 :(得分:1)

获取$ printargs test /usr/bin/python args : ViewPager的数据后,尝试用Tab设置Lat

Long

建议:您可以使用回调在private Double Lat; private Double Long; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); getLastLocation(); } private void setupViewPager() { Toast.makeText(getApplicationContext(),Double.toString(Lat), Toast.LENGTH_LONG).show(); SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), Lat, Long); ViewPager viewPager = findViewById(R.id.view_pager); viewPager.setAdapter(sectionsPagerAdapter); TabLayout tabs = findViewById(R.id.tabs); tabs.setupWithViewPager(viewPager); } @SuppressLint("MissingPermission") public void getLastLocation(){ if (checkPermissions()) { if (isLocationEnabled()) { mFusedLocationClient.getLastLocation().addOnCompleteListener( new OnCompleteListener<Location>() { @Override public void onComplete(@NonNull Task<Location> task) { Location location = task.getResult(); if (location == null) { requestNewLocationData(); } else { Lat = location.getLatitude(); Long = location.getLongitude(); // Giving the value here Toast.makeText(getApplicationContext(),"Long"+ Long, Toast.LENGTH_LONG).show(); setupViewPager(); } } } ); } else { Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } } else { requestPermissions(); } } Activity之间进行通信,而不是将数据传递给片段。选中enter image description here,以了解如何在FragmentActivity之间进行交流

答案 1 :(得分:0)

这里有很多问题。您无法以他们现在的方式来做您想做的事情。我有点惊讶这实际上编译。 Long是一个类名。我建议坚持约定,并使用小写字母。所以longitudelatitude。您的代码难以读取。

首先,一行: getLastLocation(Lat, Long);

您正在传递一个double,它是按值传递的。您没有在修改传入的值。您不能使用基本类型来做到这一点。您需要将原语封装在一个对象内并传递该对象。例如

public class Coordinates {

  private double longitude;

  private double latitude;

  // getters and setters
}

我对Android并不是很熟悉,但是似乎已经存在类似的对象,并且它在您的代码中。那就是Location。您应该使用它而不是绕过double

第二,即使您可以传递这样的基元,也要使用以下两行来创建并分配 new 变量:

final double Lat = location.getLatitude();
final double Long = location.getLongitude();

最后,您正在处理异步调用。因此,假设您解决了上述问题,您仍然会遇到问题:

mFusedLocationClient.getLastLocation().addOnCompleteListener(
            new OnCompleteListener<Location>() { ... });

不能保证此操作将在getLastLocation返回时完成,因此,当您在Toast.makeText中调用onCreate时,仍然很可能会得到0

那你能做什么?

好吧,看来您需要在调用此onCreate方法之前检索位置数据并将其缓存。然后,在此onCreate中,您可以假定它已设置。

或者,正如Mike M.建议的那样,还有另一种方法可以利用:onComplete