首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android - Map片段屏幕定向崩溃

Android - Map片段屏幕定向崩溃
EN

Stack Overflow用户
提问于 2014-01-15 12:58:26
回答 1查看 1.5K关注 0票数 0

我用了两个片段。一个是列表片段,另一个是地图片段。

这个列表显示了位置列表。当单击列表中的任何项目时,将替换片段,并使用标记在地图片段上显示位置。

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

代码语言:javascript
复制
@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 --这是它的代码:

代码语言:javascript
复制
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();
        }   
    }
}

现在的问题是,当我看到地图与当前的地方-一切都是好的!

但是当我改变设备的屏幕方向时,这个应用程序就崩溃了,它说标记丢失了位置的位置。

那么,在改变屏幕方向时,我能做些什么来显示正确的位置呢?

谢谢你的帮助。

这是一只罗格猫

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-15 13:35:22

您的错误指示:

代码语言:javascript
复制
java.lang.IllegalArgumentException: no position in marker options

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

所以你需要做的是:

代码语言:javascript
复制
 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中这样做:

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CupsLog.i(TAG, "VenueFragment onCreate");
    mVenueId = getArguments().getString(ARG_VENUEID);   
}

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

UPDATE:为您的LatLng执行以下操作,将LatLng对象拆分为两个双倍:

代码语言:javascript
复制
 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

代码语言:javascript
复制
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CupsLog.i(TAG, "VenueFragment onCreate");
    lat = getArguments().getDouble(LAT);    
    lng = getArguments().getDouble(LNG);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21138073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档