【发布时间】:2014-09-01 11:25:36
【问题描述】:
我的 Activty 代码 sn-p 是实现 GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener:
@Override
public void onConnected(Bundle bundle) {
getCurrentLocation();
}
public void getCurrentLocation() {
if (this.servicesConnected()) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mCurrentLocation = mLocationClient.getLastLocation();
ArrayList<Address> addresses = null;
try {
latitude = String.valueOf(mCurrentLocation.getLatitude());
longitude = String.valueOf(mCurrentLocation.getLongitude());
addresses = (ArrayList<Address>) gCoder.
getFromLocation(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), 1);
mLocationClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
Log.wtf("Arsnal", "" + addresses.get(0).getLocality());
} else {
Toast.makeText(this, R.string.no_address_found, Toast.LENGTH_LONG).show();
}
Log.wtf("Location", "Lat " + latitude + " - lon " + longitude);
}
}
我想要的只是在我的片段中获取字符串纬度和经度,即
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.wtf("check","Inside OnCreate of Fragment!");
Log.wtf("check","Latitude"+((MyActivuty)getActivity()).latitude+
"Longitude"+((MyActivuty)getActivity()).longitude);
}
我在这一行得到了 NPE,即
Log.wtf("check","Latitude"+((MyActivuty)getActivity()).latitude+
"Longitude"+((MyActivuty)getActivity()).longitude);
因为在片段的 OnCreate() 之后调用了 onComplete 方法。我可以解决这个问题吗? 任何帮助表示赞赏!谢谢
【问题讨论】:
-
有两种选择。 1)以共享偏好保存数据。 2)通过Bundle传递数据。
-
什么时候调用getCurrentLocation?
-
它在 OnCoonected 覆盖方法中调用
-
你的片段什么时候使用坐标?
-
您也可以使用片段的
setArgument()作为参数传递。
标签: java android android-activity android-fragments google-play-services