如何在“位置”中手动设置纬度和经度?

时间:2019-02-22 16:02:05

标签: java android

我尝试在应用程序中手动设置纬度和经度 但是,运行后让我出错

代码

public class Common {

    public static Location NewYork;
}

配置

public void setLocation(){
    Common.NewYork.setLatitude(31.963158);  //error in this Line <--
    Common.NewYork.setLongitude(35.930359);
   fragmentAdapter();
}

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.Location.setLatitude(double)' on a null object reference

1 个答案:

答案 0 :(得分:-1)

您从未初始化NewYork变量,因此在调用setLocation()时将其设置为null。您可以执行以下操作来更改它:

public class Common {
    // Create new Location with an empty provider name, it's not necessary here
    public static Location NewYork = new Location(""); 
}

OR

public void setLocation() {
    Common.NewYork = new Location(""); 
    ...
}