public class PickUpFragment extends SherlockFragment implements OnMarkerDragListener, OnCameraChangeListener
{
View rootView;
private SupportMapFragment fragment;
private GoogleMap map;
private MySupportMapFragment itsMySupportMapFragment;
RelativeLayout progress_rl;
RelativeLayout map_rl;
Button button;
public LatLng latlng;
TextView tv;
public String address = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_pickup, container, false);
progress_rl= (RelativeLayout)rootView.findViewById(R.id.progress);
map_rl = (RelativeLayout)rootView.findViewById(R.id.map_container);
//button.setOnClickListener(pickup);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
itsMySupportMapFragment = new MySupportMapFragment();
MapViewCreatedListener mapViewCreatedListener = new MapViewCreatedListener() {
@Override
public void onMapCreated() {
initPlacesGoogleMapCtrl();
}
};
itsMySupportMapFragment.itsMapViewCreatedListener = mapViewCreatedListener;
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.map, itsMySupportMapFragment);
transaction.addToBackStack(null);
transaction.commit();
}
Marker marker;
Location loc;
public void initPlacesGoogleMapCtrl() {
// Map ready, get it.
map = itsMySupportMapFragment.getMap();
// Do what you want...
map.setOnCameraChangeListener(this);
map.moveCamera(CameraUpdateFactory.zoomTo(15));
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSherlockActivity().getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider
latlng = new LatLng(location.getLatitude(),location.getLongitude());
if(loc == null)
{
loc = location;
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
//marker = map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())).draggable(true));
try {
tv = (TextView) rootView.findViewById(R.id.map_address);
Geocoder geo = new Geocoder(PickUpFragment.this.getSherlockActivity().getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.isEmpty()) {
tv.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
String addr = "";
if(addresses.get(0).getFeatureName() != null || addresses.get(0).getFeatureName().equals("null"))
{
addr += addresses.get(0).getFeatureName();
}
if(addresses.get(0).getThoroughfare() != null || addresses.get(0).getThoroughfare().equals("null") && !addresses.get(0).getThoroughfare().equals(addresses.get(0).getFeatureName()))
{
addr += " " + addresses.get(0).getThoroughfare();
}
if(addresses.get(0).getLocality() != null || addresses.get(0).getLocality().equals("null"))
{
addr += ", " + addresses.get(0).getLocality();
}
address = addr + ", " + addresses.get(0).getAdminArea();
tv.setText(address);
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
public void pickup(View v, Activity act)
{
Sync sync = new Sync();
String ad = tv.getText().toString();
sync.makeBooking(latlng, ad, getActivity());
RelativeLayout progress_rl = (RelativeLayout)act.findViewById(R.id.progress);
progress_rl.setVisibility(getView().VISIBLE);
RelativeLayout map_rl = (RelativeLayout)act.findViewById(R.id.map_container);
map_rl.setVisibility(getView().GONE);
ProgressBar spinner = (ProgressBar) progress_rl.findViewById(R.id.progress_bar);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public void onMarkerDrag(Marker arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker arg0) {
}
@Override
public void onMarkerDragStart(Marker arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCameraChange(CameraPosition arg0) {
try
{
latlng = arg0.target;
tv = (TextView) rootView.findViewById(R.id.map_address);
Async async = new Async();
async.execute();
}
catch(Exception e)
{
}
}
private class Async extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... arg0) {
try
{
Geocoder geo = new Geocoder(PickUpFragment.this.getSherlockActivity().getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latlng.latitude, latlng.longitude, 1);
if (addresses.isEmpty()) {
}
else {
if (addresses.size() > 0) {
String addr = "";
if(addresses.get(0).getFeatureName() != null && !addresses.get(0).getFeatureName().equals("null"))
{
addr += addresses.get(0).getFeatureName();
}
if(addresses.get(0).getThoroughfare() != null && !addresses.get(0).getThoroughfare().equals("null") && !addresses.get(0).getThoroughfare().equals(addresses.get(0).getFeatureName()))
{
addr += " " + addresses.get(0).getThoroughfare();
}
if(addresses.get(0).getLocality() != null && !addresses.get(0).getLocality().equals("null"))
{
addr += ", " + addresses.get(0).getLocality();
}
address = addr + ", " + addresses.get(0).getAdminArea();
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch(IOException e)
{
}
return null;
}
@Override
protected void onPreExecute()
{
tv.setText("Waiting for Location");
}
@Override
protected void onPostExecute(String result)
{
tv.setText(address);
}
}
}
TextView tv初始化方法initPlacesGoogleMapCtrl()。当我逐行调试并逐步执行时,我可以看到它是正确的值。
但是当我尝试在pickup()中使用这个值时,即使它在之前被初始化也是null。
rootView和在整个app运行期间设置的所有其他类变量在拾取方法中都是null。
任何想法为什么会被初始化然后突然它是空的?