安卓 - 地图片段屏幕方向崩溃



我正在使用两个片段。一个是列表片段,另一个是地图片段。

该列表显示地点列表。单击列表中的任何项目时,片段将被替换,地点将用标记显示在地图片段上。

按下列表中的项目(以显示地图片段)时我使用的代码如下所示:

@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();
        }   
    }
}

现在的事情是当我看到当前位置的地图时 - 一切都很好!

但是当我更改设备的屏幕方向时,应用程序崩溃,说标记缺少位置。

那么,当屏幕方向发生变化时,我该怎么做才能显示正确的位置呢?

感谢您的任何帮助。

这是日志猫

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)

错误指示:

java.lang.IllegalArgumentException: no position in marker options
原因是

当您更改屏幕方向时,传递给片段的所有参数都将丢失。

所以你需要做的是:

 public static Fragment create(String vid) {
        VenueFragment fragment = new VenueFragment();
        Bundle args = new Bundle();
        args.putString(ARG_VENUEID, vid);
        fragment.setArguments(args);
        return fragment;
    }

就像你已经用你的newInstance方法做的那样,但你没有做什么onCreate这样做:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CupsLog.i(TAG, "VenueFragment onCreate");
    mVenueId = getArguments().getString(ARG_VENUEID);   
}

基本上,您需要获取在实例化时传递的数据并将其保存在本地。

更新:对于您的 LatLng 情况,这样做,将 LatLng 对象划分为两个双精度:

 public static Fragment create(double lat, double lng) {
    VenueFragment fragment = new VenueFragment();
    Bundle args = new Bundle();
    args.putDouble(LAT, lat)
    args.putDouble(LNG, lng)
    fragment.setArguments(args);
    return fragment;
}

onCreate

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CupsLog.i(TAG, "VenueFragment onCreate");
    lat = getArguments().getDouble(LAT);    
    lng = getArguments().getDouble(LNG);
}

最新更新