android: NullPointerException FragmentManagerImpl.moveToState(FragmentManager.java) on onResume()

asked 2014-12-15 05:34:23 -0600

Asif Khan gravatar image

my app work fine but when i'm trying to reclick the current running fragment from navigation drawer. my app crashes. it only happens in one fragment. MAP fragment. i got the error in onResume method. here is my code

package com.munawwar.sultan.NavDrawer;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.munawwar.sultan.R;

import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MapzFragment extends Fragment implements LocationListener {
private GoogleMap mMap;
private LocationManager locationManager;

View view;

public MapzFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_mapz, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return view;
}

/*
 * RelativeLayout theLayout = (RelativeLayout) inflater.inflate(
 * R.layout.fragment_mapz, container, false); return theLayout; }
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

} // ending on activity created

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (mMap != null) {
        return;
    }
    mMap = ((MapFragment) getActivity().getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    // Enabling MyLocation Layer of Google Map
    mMap.setMyLocationEnabled(true);

    // Getting LocationManager object from System Service LOCATION_SERVICE
    locationManager = (LocationManager) getActivity().getSystemService(
            Context.LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // provider = locationManager.getBestProvider(criteria, false);
    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);
    // ***************current location*******************
    if (location != null) {
        onLocationChanged(location);
    }
    // location update
    locationManager.requestLocationUpdates(provider, 2000, 0,
            (LocationListener) this);
    // ****************map*************************
    if (mMap == null) {
        return;
    }

    // Initialize map options. For example:
    // mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    // ***********RADIO BUTTONS FOR CHANGING MAP************
    RadioGroup rgViews = (RadioGroup) getActivity().findViewById(
            R.id.rg_views);
    rgViews.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.rb_hybrid) {
                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            } else if (checkedId == R.id.rb_satellite) {
                mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                mMap.setTrafficEnabled(true);
            } else if (checkedId == R.id.rb_terrain) {
                mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                mMap.setTrafficEnabled(true);
            }
        }
    });

}

// *********on location listener implemented functions
@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(MapzFragment.this.getActivity(),
            "Disabled provider " + provider, Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(MapzFragment.this.getActivity(),
            "Enabled new provider " + provider, Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}

@Override
public void onLocationChanged(Location location) {

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude ...
(more)
edit retag flag offensive close merge delete