gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:13:31 25 4
gpt4 key购买 nike

请帮忙。我不是一个普通的java作家,所以我无法解决这个问题:onCreate 方法中的 getLastLocation() 对于 LatLang 返回 0.0,而该值在 code>getLastLocation() 方法本身。另外,AndroidStudio 表示,在 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();
}
}

最佳答案

获取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 之间进行通信,而不是将数据传递给 Fragment。检查this了解如何在ActivityFragment之间进行通信

关于java - 访问内部类变量到另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com