我正在使用兩個片段。一個是列表片段,另一個是地圖片段。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的..! –