我用了两个片段。一个是列表片段,另一个是地图片段。
这个列表显示了位置列表。当单击列表中的任何项目时,将替换片段,并使用标记在地图片段上显示位置。
当按下列表中的项时(为了显示地图片段),我使用的代码如下所示:
@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)发布于 2014-01-15 13:35:22
您的错误指示:
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);
}基本上,您需要获取实例化时传递的数据并将其保存在本地。
UPDATE:为您的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);
}https://stackoverflow.com/questions/21138073
复制相似问题