문제

I have a layout with ActionBar.NAVIGATION_MODE_TABS. The first tab is to display the results in listview and the second in a map. Everything worked great until I have upgraded to the latest version of Google Play Services lib.

Here the fragment used to display the map:

 public class PlaceMapFragment extends Fragment {

    MapView mapView;
GoogleMap map;
ResultsListener mCallback;
List<Place> places;
private HashMap<String, Place> placeMarker = new HashMap<String, Place>();

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

    places = mCallback.getPlaces();

    View rootView = inflater.inflate(R.layout.mapview, container, false);

    mapView = (MapView) rootView.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    map = mapView.getMap();

    setUpMap();

    return rootView;
}

private void setUpMap() {
    map.getUiSettings().setMyLocationButtonEnabled(true); 
    map.setMyLocationEnabled(true);
    /*
    try { 
        MapsInitializer.initialize(this.getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
    */

   // add markers

}

@Override 
public void onResume() { 
    mapView.onResume();
    super.onResume();

    setUpMap();
}

@Override 
public void onPause() { 
   mapView.onPause();
   super.onPause();
}

@Override
public void onDestroy() {
    mapView.onDestroy();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

public interface ResultsListener {
    public List<Place> getPlaces();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (ResultsListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement ResultsListener");
    }
}
}

I get these errors:

04-05 03:58:22.040: E/AndroidRuntime(661): FATAL EXCEPTION: main
04-05 03:58:22.040: E/AndroidRuntime(661): java.lang.NullPointerException
04-05 03:58:22.040: E/AndroidRuntime(661):  at com.tomsyweb.mapslee.fragments.PlaceMapFragment.setUpMap(PlaceMapFragment.java:54)
04-05 03:58:22.040: E/AndroidRuntime(661):  at com.tomsyweb.mapslee.fragments.PlaceMapFragment.onCreateView(PlaceMapFragment.java:48)

I have commented the MapsInitializer.initialize(this.getActivity()); part because GooglePlayServicesNotAvailableException gives error.

mapview.xml

<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

My code is taken from Getting Google Map Fragment in onCreateView using ActionBar tabs

도움이 되었습니까?

해결책

Your map may be null if the device doesn't have the latest version of Maps installed, or perhaps for other reasons. Trying to create it may even prompt the user to start a different activity to update their device. So it's recommended to create an initMap() method like this, and call it in your onCreate(...) and onResume():

private void initMap() {
   if(map == null) {
      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
      if(map != null) {
         // set up map
      }
   }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top