我Activty代碼片段是實現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);
因爲在片段的OnC之後調用onComplete方法reate()。我可以解決這個問題嗎? 任何幫助表示讚賞感謝
有兩種選擇。 1)將數據保存在共享首選項中。 2)通過捆綁傳遞數據。 – Piyush 2014-09-01 11:32:11
何時調用getCurrentLocation? – Simas 2014-09-01 11:37:58
它被稱爲OnCoonectedOveridden方法 – goonerDroid 2014-09-01 11:39:55