请帮助。我不是普通的Java作家,所以我无法解决此问题:getLastLocation()
方法中的onCreate
对于Lat
和Lang
返回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();
}
}
最佳答案
在获取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
和Fragment
之间进行通信,而不是将数据传递给片段。选中this,了解如何在Activity
和Fragment
之间进行通信