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



请帮忙。我不是一个普通的Java作家,所以我无法解决这个问题: 方法onCreategetLastLocation()是返回 0.0 表示LatLang,而该方法本身的值是正确的getLastLocation()。此外,在getLastLocation中,AndroidStudio说,从未使用过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();
}
}

在获取LatLong的数据后,尝试使用Tab设置ViewPager

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();
}
}

建议:与其将数据传递到片段,不如使用回调在ActivityFragment之间进行通信。查看此内容以了解如何在ActivityFragment之间进行通信

你在这里有很多问题。你不能像现在这样做你想做的事情。我有点惊讶这实际上编译了。Long是一个类名。我建议坚持惯例,并使用小写。所以longitudelatitude.您的代码读起来令人困惑。

一、行:getLastLocation(Lat, Long);

您正在传递一个按值传递的double。您没有修改传入的值。不能对基元类型执行此操作。您需要将基元封装在对象中并传递该对象。例如

public class Coordinates {
private double longitude;
private double latitude;
// getters and setters
}

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

其次,即使你可以传递这样的基元,你也在用这两行创建和分配新的变量:

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

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

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

无法保证此操作将在getLastLocation返回时完成,因此当您在onCreate中呼叫Toast.makeText时,您仍然可能会得到0

那你能做什么呢?

好吧,似乎您需要在调用此onCreate方法之前检索位置数据并缓存它。然后,在这个onCreate中,你可以假设它已设置。

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

最新更新