我的应用选择列表视图中的一个对象并显示地图。工作在第一次运行,但在第二次没有工作。第三,它有效!
这是因为第二次出现了膨胀异常。它无法返回视图或对其进行充气。
我需要每次选中它时导航抽屉带来的地图,我使用替换和许多问题onDestroy中提到的方法,并且当它与null不同时显然删除了视图。
帮助将不胜感激!!谢谢!
地图代码:
public class mapfragmentplaces extends Fragment implements LocationListener {
protected LocationManager locationManager;
// GoogleMap googleMap;
LatLng myPosition;
private SupportMapFragment map;
private GoogleMap mMapView;
int fragVal2;
static Context ontext;
private static View view;
//lo que se ocupa para construir los sitios
String[] mPlaceType=null;
String[] mPlaceTypeName=null;
double mLatitude=0;
double mLongitude=0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_layout, container, false);
android.support.v4.app.FragmentManager fm = getFragmentManager();
/*
GoogleMapOptions gmo = (new GoogleMapOptions()).zoomControlsEnabled(false).rotateGesturesEnabled(false);
SupportMapFragment mapFragment = SupportMapFragment.newInstance(gmo);
android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager() .beginTransaction();
fragmentTransaction.add(R.id.mapFragmentHole, mapFragment);
fragmentTransaction.commit();
GoogleMap map = mapFragment.getMap(); */
map = (SupportMapFragment) fm.findFragmentById(R.id.map);
/* if (map == null) {
map = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, map).commit();
} */
// mMapView = fm.getMap();
mMapView = map.getMap();
// Enabling MyLocation Layer of Google Map
mMapView.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location From GPS
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
mMapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(getActivity(), Stores.class);
startActivity(intent);
}
});
// Clears all the existing markers
mMapView.clear();
String Lugar = "Plaza fiesta";
String Desc = "Lugar bonito";
Double Lat3 = 25.751188242782035;
Double Lng3 = -100.3097140789032;
// notificacion(Results.toString());
mMapView.addMarker(new MarkerOptions()
.position(new LatLng(Lat3,Lng3))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1))
.title(Lugar)
.snippet(Desc));
List<HashMap<String, String>> list = setPlaceTag.getList();
if (list!=null){
//((Menu) map).clear();
for(int i=0;i<list.size();i++){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting a place from the places list
HashMap<String, String> hmPlace = list.get(i);
// Getting latitude of the place
double lat = Double.parseDouble(hmPlace.get("lat"));
// Getting longitude of the place
double lng = Double.parseDouble(hmPlace.get("lng"));
// Getting name
String name = hmPlace.get("place_name");
// Getting vicinity
String vicinity = hmPlace.get("vicinity");
LatLng latLng = new LatLng(lat, lng);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(name + " : " + vicinity);
// Placing a marker on the touched position
mMapView.addMarker(markerOptions);
}
}
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
@Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
LatLng latLng = new LatLng(mLatitude, mLongitude);
mMapView.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMapView.animateCamera(CameraUpdateFactory.zoomTo(12));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
if (fragment != null)
getFragmentManager().beginTransaction().remove(fragment).commit();
}
}
在我的主要活动中,我用这个来调用mapfragmentplaces:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//if (dataList.get(position).getTitle() == null) {
//SelectItem(position);
// } //Toast.makeText(getApplicationContext(), "diste click a sitios", Toast.LENGTH_SHORT).show();
//Currently selected item in spinner first is airport!!!
mTitle = setPlaceTag.getTag() ;
// Creating a fragment object
mapfragmentplaces rFragment = new mapfragmentplaces();
// Creating a Bundle object
Bundle data = new Bundle();
// Setting the index of the currently selected item of mDrawerList
data.putString("position", setPlaceTag.getTag());
// Setting the position to the fragment it is the value of the spinner
rFragment.setArguments(data);
// Getting reference to the FragmentManager
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
// Creating a fragment transaction
android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
// Adding a fragment to the fragment transaction
ft.replace(R.id.content_frame, rFragment);
// Committing the transaction
ft.commit();
// Closing the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
}
我的地图xml:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
答案 0 :(得分:0)
检查你的onCreateView方法。我不认为它是在oncreateview方法中膨胀xml文件的正确方法。请试试这个:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout.,container,false);
//start inflating the view and return the view
map = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
}
利用和使用视图方法的好地方是片段的开始。
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
mMapView = map.getMap();
}
此外,我看到您正在混合片段中的支持包。见这一行:
android.support.v4.app.FragmentManager fm = getFragmentManager();
确保片段的所有导入和代码都来自支持包或默认包(api 11或更高版本)。这是像classcastexception这样的bug的常见来源。