Я использую два фрагмента. Один - это фрагмент списка, а другой - фрагмент карты.Android - Сбой ориентации экрана фрагмента карты
В списке показаны списки мест. Когда щелкнут любой элемент из списка, фрагмент будет заменен, и место будет показано на фрагменте карты с помощью маркера.
код, который я использую, когда элемент в списке нажат (для того, чтобы показать фрагмент карты), как это:
@Override
public void onPlace(Double lan, Double lat, String name) {
// Setting a LatLng variables according to the lan and lat that the
// function received
LatLng place = new LatLng(lan, lat);
// Getting the zoom by the user choice
SharedPreferences userPref = PreferenceManager.getDefaultSharedPreferences(this);
if (userPref != null) {
zooMao = Integer.parseInt(userPref.getString("zoomsize", "17"));
}
GoogleMapOptions options = new GoogleMapOptions();
options.camera(CameraPosition.fromLatLngZoom(place, zooMao));
options.mapType(GoogleMap.MAP_TYPE_SATELLITE);
Mapf mapFrag = Mapf.newInstance(place, name, options);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.phone_cont, mapFrag, "map");
ft.addToBackStack(null);
ft.commit();
}
Как вы можете видеть, я использую свой собственный фрагмент карты код - с именем -Mapf - и это код для него:
public class Mapf extends SupportMapFragment {
private LatLng position;
private Marker marker;
private String name;
// Creating an instance of the map fragment
public static Mapf newInstance(LatLng pos, String place, GoogleMapOptions option){
// Declaring an instance of the map object
Mapf fragment = new Mapf();
//Adding data to the fragment
// such as position, name and map options
Bundle args = new Bundle();
//obtained by decompiling google-play-services.jar
args.putParcelable("MapOptions", option);
fragment.name = place;
fragment.position = pos;
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
initMap();
return v;
}
// This function adding marker and location name and position to the map
private void initMap(){
// If there's a marker - removing it
if (marker != null) {
marker.remove();
}
// Declaring icon for the marker
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.icon);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(position);
markerOptions.title(name);
markerOptions.icon(icon);
marker = getMap().addMarker(markerOptions);
// in case there's no name for the place - no need in a marker
// using for the first loading of the map fragment
if(name.equals("")){
marker.remove();
}
}
}
Теперь дело в том, когда я вижу карту с текущим местом - все отлично!
Но когда я меняю ориентацию экрана устройства, приложение терпит крах, заявив, что маркеру не хватает позиции места.
Итак, что я могу сделать, чтобы показать нужное место при изменении ориентации экрана?
Спасибо за любую помощь.
и вот LogCat
01-15 13:22:06.012: E/AndroidRuntime(1492): FATAL EXCEPTION: main
01-15 13:22:06.012: E/AndroidRuntime(1492): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myplaces1/com.example.myplaces1.view.MainActivity}: java.lang.IllegalArgumentException: no position in marker options
01-15 13:22:06.012: E/AndroidRuntime(1492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
пост LogCat ..! –