android - Why am I getting NullPointerException with FusedLocationProviderClient when using properties from other class
问题描述
Excuse me for the basic question but I am learning android programming now.
I am using GoogleFusedProviderClient to get the location of the app from a custom class. I have put it in a class as I need to call this from multiple activities.
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();
}
}
});
}
On my Activity I call getAppLocation() to get the location and then try to use Fas.latitude
and Fas.longitude
. This is where I am getting a null pointer exception and do not understand the reason.
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);
}
解决方案
Because OnSuccessListener
is a asynchronous, printing the latitude will be executed before the executing of onSuccess
you need to use callbacks .
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);
}
and :
private void function check_loc() {
Fas.getAppLocation();
fas.setOnLocationLoadedListener(new fas.onLocationLoadedListener() {
@Override
public void onLocationLoaded(Location location) {
System.out.println(location.latitude);
}
});
}
推荐阅读
- reactjs - 使用自定义扩展加载资产不起作用
- c++ - QNetworkaccessmanager 对象连接函数不调用
- asp.net - 如何在单个 Web api ASP.NET Core 中发布 json 和文件对象
- javascript - 在返回结果之前等待ajax完成(通用功能)
- excel - 仅将 VBA 粘贴到可见单元中优化
- openssl - 如何为mingw编译器编译fips?
- java - 在黄瓜框架的报告中生成 json 后在哪里调用 jar 文件(ATM)?
- javascript - 根据角度js中的数据过滤对象
- angular - 如何在 Angular 5 中检查显示列表
- c - C程序中的两个文件在一列中