您好,我想获得准确的位置,以便我使用以下链接
在 Android 中获取用户当前位置的最简单、最可靠的方法是什么?
以下是该站点的代码:
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
现在在LocationResult
课堂上我已经修改了它并想在文本视图中显示位置所以我写了下面的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
TextView txtValue = (TextView)findViewById(R.id.txtValue);
txtValue.setText("LATITUDE : - "+location.getLatitude()+"\n\n LONGITUDE :- "+location.getLongitude()+"\n\n ALTITUDE :- "+location.getAltitude());
}
};
myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
但在那我得到了例外
下面是我的日志
10-04 17:06:35.699: E/AndroidRuntime(5733): FATAL EXCEPTION: Timer-0
10-04 17:06:35.699: E/AndroidRuntime(5733): java.lang.NullPointerException
10-04 17:06:35.699: E/AndroidRuntime(5733): at com.example.gps_accurate_data.MainActivity$1.gotLocation(MainActivity.java:22)
10-04 17:06:35.699: E/AndroidRuntime(5733): at com.example.gps_accurate_data.MyLocation$GetLastLocation.run(MyLocation.java:119)
10-04 17:06:35.699: E/AndroidRuntime(5733): at java.util.Timer$TimerImpl.run(Timer.java:284)