我有导航条(菜单滑块),可以为您提供google api中的位置。
当您选择抽屉列表的某个位置时,应替换新地图。 地图是在xml上定义的。
问题在于,当它第二次被替换时,它会将片段空白....第三次它被很好地替换(?)。我需要的是,如果每次我选择一个新的地方,地图都会成功替换方法,而不是第三次。
当第二次更换时,它会进入膨胀异常....检查许多答案告诉我为视图验证添加的方法应该有效......但它没有。还有关于destroyView的方法..就像那个方法不能很好地删除片段一样..
这是我的地图片段地点:
package google.Bi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.app.Dialog;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class mapfragmentplaces extends Fragment implements LocationListener, OnClickListener {
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) {
//setSpinnerContent( view );
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
view = null;
}
}
try {
view = inflater.inflate(R.layout.fragment_layout, container, false);
final List<String> list=new ArrayList<String>();
list.add("Nuevo Leon");
final List<String> list2=new ArrayList<String>();
list2.add("Monterrey");
final List<String> list3=new ArrayList<String>();
list3.add("Plaza Fiesta Anahuac");
//final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
final Spinner sp1= (Spinner) view.findViewById(R.id.Spinner01);
final Spinner sp3= (Spinner) view.findViewById(R.id.Spinner02);
final Spinner sp2= (Spinner) view.findViewById(R.id.Spinner03);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
ArrayAdapter<String> adp2=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_spinner_item,list2);
adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adp2);
ArrayAdapter<String> adp3=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_spinner_item,list3);
adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adp3);
Button b = (Button) view.findViewById(R.id.button);
b.getBackground().setAlpha(75);
b.setOnClickListener(this);
android.support.v4.app.FragmentManager fm = getFragmentManager();
map = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (map == null) {
map = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, map).commit();
}
mMapView = map.getMap();
Double Lat10 = 25.751188242782035;
Double Lng10 = -100.3097140789032;
LatLng latLng5 = new LatLng(Lat10, Lng10);
mMapView.moveCamera(CameraUpdateFactory.newLatLng(latLng5));
mMapView.animateCamera(CameraUpdateFactory.zoomTo(12));
// 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);
// Clears all the existing markers
mMapView.clear();
final 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));
/* mMapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(getActivity(), Stores.class);
startActivity(intent);
}
}); */
mMapView.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// TODO Auto-generated method stub
Intent intent2 = null;
if(marker.getTitle().equals(Lugar)) // if marker source is clicked
{ intent2 = new Intent(getActivity(), Stores2.class);
startActivity(intent2);
} else
{
marker.showInfoWindow();
}
return true;
}
});
List<HashMap<String, String>> list5 = setPlaceTag.getList();
String we[] = setPlaceTag.getFinals2();
String we2[] = setPlaceTag.getFinals3();
LatLng we3[] = setPlaceTag.getFinals4();
if (list5!=null){
//((Menu) map).clear();
for(int i=0;i<list5.size();i++){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting a place from the places list
HashMap<String, String> hmPlace = list5.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);
if( we[setPlaceTag.getPosition()].equals(name) && we2[setPlaceTag.getPosition()].equals(vicinity)
&& we3[setPlaceTag.getPosition()].equals(latLng)){
markerOptions.position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
}
// Placing a marker on the touched position
mMapView.addMarker(markerOptions);
}
}
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
/*if(anterior!=actual){
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
getFragmentManager().beginTransaction().remove(fragment).commit();
} */
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Double Lat3 = 25.751188242782035;
Double Lng3 = -100.3097140789032;
LatLng latLng100 = new LatLng(Lat3, Lng3);
mMapView.moveCamera(CameraUpdateFactory.newLatLng(latLng100));
mMapView.animateCamera(CameraUpdateFactory.zoomTo(12));
break;
}
}
@Override
public void onLocationChanged(Location location) {
// mLatitude = location.getLatitude();
// mLongitude = location.getLongitude();
// LatLng latLng15 = new LatLng(mLatitude, mLongitude);
/* Double Lat3 = 25.751188242782035;
Double Lng3 = -100.3097140789032;
LatLng latLng = new LatLng(Lat3, Lng3);
mMapView.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMapView.animateCamera(CameraUpdateFactory.zoomTo(12));*/
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@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();
map = SupportMapFragment.newInstance();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
if (fragment != null){
getFragmentManager().beginTransaction().remove(fragment).commit();
}
}
}
以下是我在主要活动中替换它的方法:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Currently selected item in spinner first is airport!!!
mTitle = setPlaceTag.getTag() ;
setPlaceTag.setPosition(position - 1);
// Creating a fragment object
mapfragmentplaces rFragment = new mapfragmentplaces();
// 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:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<!-- <FrameLayout
android:id="@+id/mapFragmentHole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="60dp"
android:layout_gravity="center_vertical|center_horizontal" /> -->
<TextView
android:id="@+id/tv_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textSize="40sp"
/>
<Spinner
android:id="@+id/Spinner01"
android:layout_width="146dp"
android:layout_height="34dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="30dp"
android:drawSelectorOnTop="true" />
<Spinner
android:id="@+id/Spinner02"
android:layout_width="146dp"
android:layout_height="34dp"
android:layout_alignLeft="@+id/Spinner03"
android:layout_below="@+id/Spinner03" />
<Spinner
android:id="@+id/Spinner03"
android:layout_width="146dp"
android:layout_height="34dp"
android:layout_alignLeft="@+id/Spinner01"
android:layout_below="@+id/Spinner01" />
<Button
android:id="@+id/button"
style="@style/CustomStyleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Spinner02"
android:layout_below="@+id/Spinner02"
android:text="Go" />
</RelativeLayout>
我已经在这个问题上做了答案:
Error inflating class fragment - duplicate id/illegalargumentexception?
但没有人工作......任何人都可以帮忙吗? ......谢谢。