如何手动设置位置中的纬度和经度?

我试图在应用程序中手动设置纬度和经度但是,运行后给我一个错误


代码


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



扬帆大鱼
浏览 140回答 1
1回答

UYOU

您从未初始化NewYork变量,因此在setLocation()调用时将其设置为 null。您可以通过以下方式更改:public class Common {&nbsp; &nbsp; // Create new Location with an empty provider name, it's not necessary here&nbsp; &nbsp; public static Location NewYork = new Location("");&nbsp;}或者public void setLocation() {&nbsp; &nbsp; Common.NewYork = new Location("");&nbsp;&nbsp; &nbsp; ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java