我想在活动中使用 mapfragment ,所以我创建了地图的xml文件和java文件,我在中调用它主要活动 y但我收到此错误
java.lang.StackOverflowError的 在这一行
View view = inflater.inflate(R.layout.fragment_map, container, false);
06-29 11:14:26.264 23599-23599 / com.sifast.appsocle E / AndroidRuntime: 致命异议:主要 java.lang.StackOverflowError的 在 android.content.res.AssetManager.getResourceValue(AssetManager.java:201) 在android.content.res.Resources.getValue(Resources.java:1022) 在 android.content.res.Resources.loadXmlResourceParser(Resources.java:2131) 在android.content.res.Resources.getLayout(Resources.java:865) 在android.view.LayoutInflater.inflate(LayoutInflater.java:394) at com.sifast.appsocle.mapFrag.onCreateView(mapFrag.java:69) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 在 android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332) 在 android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288) 在 android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 在 android.view.LayoutInflater $ FactoryMerger.onCreateView(LayoutInflater.java:168) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 在android.view.LayoutInflater.inflate(LayoutInflater.java:466) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.sifast.appsocle.mapFrag.onCreateView(mapFrag.java:69) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 在 android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332) 在 android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288) 在 android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 在 android.view.LayoutInflater $ FactoryMerger.onCreateView(LayoutInflater.java:168) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 在android.view.LayoutInflater.inflate(LayoutInflater.java:466) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.sifast.appsocle.mapFrag.onCreateView(mapFrag.java:69) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 在 android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332) 在 android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288) 在 android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 在 android.view.LayoutInflater $ FactoryMerger.onCreateView(LayoutInflater.java:168) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 在android.view.LayoutInflater.inflate(LayoutInflater.java:466) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.sifast.appsocle.mapFrag.onCreateView(mapFrag.java:69) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 在 android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332) 在 android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288) 在 android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 在 android.view.LayoutInflater $ FactoryMerger.onCreateView(LayoutInflater.java:168) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 在android.view.LayoutInflater.inflate(LayoutInflater.java:466) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.sifast.appsocle.mapFrag.onCreateView(mapFrag.java:69) 在android.support.v4.app.Fragment.performCreateView(Fragment.java
这是我的xml和java
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-au
to"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.sifast.appsocle.mapFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sifast.appsocle.mapFrag"
/>
java代码
public class mapFrag extends android.support.v4.app.Fragment implements OnMapReadyCallback, LocationListener {
public static FragmentManager fragmentManager;
private GoogleMap map;
private LocationManager locationManager;
private boolean isGPSEnabled, isNetworkEnabled, isGPSTrackingEnabled;
private String provider_info;
private static String TAG = MapsActivity.class.getName();
private Location location;
private double latitude;
private double longitude;
// The minimum distance to change updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public mapFrag() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
getMyLocation();
return view;
}
@Override
public void onLocationChanged(Location location) {
//txtOutput.setText(location.toString());
System.out.println(String.valueOf(location.getLatitude()));
System.out.println(String.valueOf(location.getLongitude()));
try {
map.clear();
} catch (Exception e) {
}
LatLng mypos = new LatLng(location.getLatitude(), location.getLongitude());
//camera annimation
CameraPosition camPos = new CameraPosition.Builder().target(mypos)
.zoom(70)
.bearing(45)
.tilt(65)
.build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
map.animateCamera(camUpd3);
map.addMarker(new MarkerOptions().position(mypos).title("here i am !!"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(mypos));
//map.moveCamera(CameraUpdateFactory.newLatLng(mypos));
}
@Override
public void onMapReady(GoogleMap googleMap) {
map= googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
return;
}
void getMyLocation(){
// definition of the location manager
LocationManager locManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
//definition of the listenner
LocationListener locationListener = new LocationListener() {
// Called when a new location is found by the network location provider.
public void onLocationChanged(Location location) {
// setting the camera of the map to positionate the marker in the current position
LatLng myLaLn = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition camPos = new CameraPosition.Builder().target(myLaLn)
.zoom(15)
.bearing(45)
.tilt(70)
.build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
map.animateCamera(camUpd3);
// setting the marker in the current position
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("My position")
.snippet("Population: 776733"));
}
};
}}
这就是我在on create function中调用它的方式
mapFrag myMap = new mapFrag();
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.myHomeContainer,myMap,"test");
transaction.commit();