为什么我在使用来自其他 class 的属性时使用 FusedLocationProviderClient 得到 NullPointerException
Why am I getting NullPointerException with FusedLocationProviderClient when using properties from other class
请问这个基本问题,但我现在正在学习 android 编程。
我正在使用 GoogleFusedProviderClient 从自定义 class 获取应用程序的位置。我把它放在 class 中,因为我需要从多个活动中调用它。
fas class:
public class fas {
// get the context from the Activity that uses this class
private Context context;
// Vars to store GPS info
public Double latitude, longitude;
public Float accuracy;
private FusedLocationProviderClient flpc;
fas_functions(Context context) {
this.context = context;
}
public void getAppLocation() {
flpc = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Already forced user to enable location
return;
}
flpc.getLastLocation().addOnSuccessListener((Activity) context,
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
} else {
// No position so request an immediate one
startListener();
}
}
});
}
在我的 Activity 上,我调用 getAppLocation() 获取位置,然后尝试使用 Fas.latitude
和 Fas.longitude
。这是我得到空指针异常并且不明白原因的地方。
Activity:
public class Activity extends AppCompatActivity {
.......
// Import Class fas_functions
fas_functions Fas = new fas_functions(this);
........
private void function check_loc() {
Fas.getAppLocation();
// This will return null and if I use this in Volley for example it will throw a NullPointerException
System.out.println(Fas.latitude);
}
因为OnSuccessListener
是异步的,打印纬度会在执行onSuccess
之前执行,需要使用回调。
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
onLocationLoadedListener.onLocationLoaded(location);
} else {
// No position so request an immediate one
startListener();
}
}
});
private onLocationLoadedListener onLocationLoadedListener;
public void setOnLocationLoadedListener(fas.onLocationLoadedListener onLocationLoadedListener) {
this.onLocationLoadedListener = onLocationLoadedListener;
}
public interface onLocationLoadedListener{
void onLocationLoaded(Location location);
}
和:
private void function check_loc() {
Fas.getAppLocation();
fas.setOnLocationLoadedListener(new fas.onLocationLoadedListener() {
@Override
public void onLocationLoaded(Location location) {
System.out.println(location.latitude);
}
});
}
请问这个基本问题,但我现在正在学习 android 编程。
我正在使用 GoogleFusedProviderClient 从自定义 class 获取应用程序的位置。我把它放在 class 中,因为我需要从多个活动中调用它。
fas class:
public class fas {
// get the context from the Activity that uses this class
private Context context;
// Vars to store GPS info
public Double latitude, longitude;
public Float accuracy;
private FusedLocationProviderClient flpc;
fas_functions(Context context) {
this.context = context;
}
public void getAppLocation() {
flpc = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Already forced user to enable location
return;
}
flpc.getLastLocation().addOnSuccessListener((Activity) context,
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
} else {
// No position so request an immediate one
startListener();
}
}
});
}
在我的 Activity 上,我调用 getAppLocation() 获取位置,然后尝试使用 Fas.latitude
和 Fas.longitude
。这是我得到空指针异常并且不明白原因的地方。
Activity:
public class Activity extends AppCompatActivity {
.......
// Import Class fas_functions
fas_functions Fas = new fas_functions(this);
........
private void function check_loc() {
Fas.getAppLocation();
// This will return null and if I use this in Volley for example it will throw a NullPointerException
System.out.println(Fas.latitude);
}
因为OnSuccessListener
是异步的,打印纬度会在执行onSuccess
之前执行,需要使用回调。
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
onLocationLoadedListener.onLocationLoaded(location);
} else {
// No position so request an immediate one
startListener();
}
}
});
private onLocationLoadedListener onLocationLoadedListener;
public void setOnLocationLoadedListener(fas.onLocationLoadedListener onLocationLoadedListener) {
this.onLocationLoadedListener = onLocationLoadedListener;
}
public interface onLocationLoadedListener{
void onLocationLoaded(Location location);
}
和:
private void function check_loc() {
Fas.getAppLocation();
fas.setOnLocationLoadedListener(new fas.onLocationLoadedListener() {
@Override
public void onLocationLoaded(Location location) {
System.out.println(location.latitude);
}
});
}