在xml中为Google地图使用片段,显示错误为;android.view.InflateException:二进制XML文件行“57:膨胀类片段时出错”;
我正在做一个项目,我需要在主屏幕上显示地图。我使用了fragment类。 当我第一次在屏幕上吃午饭时,它成功地显示了地图。但当我再次点击主页或再次从另一个屏幕返回地图屏幕时,下面的错误显示出来,应用程序崩溃在xml中为Google地图使用片段,显示错误为;android.view.InflateException:二进制XML文件行“57:膨胀类片段时出错”;,android,android-fragments,dictionary,Android,Android Fragments,Dictionary,我正在做一个项目,我需要在主屏幕上显示地图。我使用了fragment类。 当我第一次在屏幕上吃午饭时,它成功地显示了地图。但当我再次点击主页或再次从另一个屏幕返回地图屏幕时,下面的错误显示出来,应用程序崩溃 "android.view.InflateException: Binary XML file line #57: Error inflating class fragment" 这是我的xml类 <RelativeLayout xmlns:android="http://schem
"android.view.InflateException: Binary XML file line #57: Error inflating class fragment"
这是我的xml类
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/homemap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
map:cameraTilt="50"
map:cameraZoom="40" />
</RelativeLayout>
下面是我的Java类
public class Customer_Home_Map extends Fragment {
public Customer_Home_Map() {
}
private GoogleMap theMap;
ProgressDialog progressDialog = null;
ArrayList<SafetyResults_SearchResult> lstresult;
double lati = 0.0, longi = 0.0;
GPSTracker gps;
GoogleMap mMap;
SharedPreferences sharedPreferences;
int locationCount = 0;
API api = API.getInstance();
private HashMap<Marker, MyMarker> mMarkersHashMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = inflater.inflate(R.layout.customer_home_map,
// container,
// false);
View view = inflater.inflate(R.layout.customer_home_map,
null);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
up.getFont_Lato_Regular());
gps = new GPSTracker(getActivity());
showAddress();
return view;
}
private void showAddress() {
if (gps.canGetLocation()) {
lati = gps.getLatitude();
longi = gps.getLongitude();
straddress = getMyLocationAddress(lati, longi);
}
mMarkersHashMap = new HashMap<Marker, MyMarker>();
setUpMap();
plotMarkers(mMyMarkersArray);
}
private void plotMarkers(ArrayList<MyMarker> markers) {
if (markers.size() > 0) {
for (MyMarker myMarker : markers) {
// Create user marker with custom icon and other options
MarkerOptions markerOption = new MarkerOptions()
.position(new LatLng(myMarker.getmLatitude(), myMarker
.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
private void setUpMap() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.homemap)).getMap();
LatLng coordinate = new LatLng(lati, longi);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(
coordinate, 15);
mMap.animateCamera(yourLocation);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(
com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
return true;
}
});
} else
Toast.makeText(getActivity().getApplicationContext(),
"Unable to create Maps", Toast.LENGTH_SHORT).show();
}
}
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
public MarkerInfoWindowAdapter() {
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(
R.layout.infowindow_layout, null);
MyMarker myMarker = mMarkersHashMap.get(marker);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
TextView markerLabel = (TextView) v.findViewById(R.id.marker_label);
markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
markerLabel.setText(myMarker.getmLabel());
TextView anotherLabel = (TextView) v
.findViewById(R.id.another_label);
// anotherLabel.setText("A custom text");
return v;
}
}
private int manageMarkerIcon(String markerIcon) {
if (markerIcon.equalsIgnoreCase("3"))
return R.drawable.ambulance;
else if (markerIcon.equalsIgnoreCase("4"))
return R.drawable.doctor;
else if (markerIcon.equalsIgnoreCase("me"))
return R.drawable.marker;
else
return R.drawable.marker;
}
公共类客户家庭地图扩展片段{
公共客户家庭地图(){
}
私人谷歌地图;
ProgressDialog=null;
阵列列表结果;
双纬度=0.0,经度=0.0;
全球定位系统;
谷歌地图;
SharedReferences SharedReferences;
int locationCount=0;
API=API.getInstance();
私有HashMap-mMarkersHashMap;
private ArrayList mMyMarkersArray=new ArrayList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//视图根视图=充气机。充气(R.layout.customer\u home\u map,
//集装箱,
//假);
视图=充气机。充气(右布局。客户\主页\地图,
无效);
Typeface tf=Typeface.createFromAsset(getActivity().getAssets(),
getFont_Lato_Regular());
gps=新的GP斯特拉克(getActivity());
showAddress();
返回视图;
}
私有地址(){
if(gps.canGetLocation()){
lati=gps.getLatitude();
longi=gps.getLongitude();
StradAddress=getMyLocationAddress(lati,longi);
}
mMarkersHashMap=新HashMap();
setUpMap();
绘图标记(mmyMarkerArray);
}
专用空心打印标记(ArrayList标记){
如果(markers.size()>0){
用于(MyMarker MyMarker:markers){
//使用自定义图标和其他选项创建用户标记
MarkerOptions markerOption=新的MarkerOptions()
.位置(新板条)(myMarker.getmLatitude(),myMarker
.getmlongity());
图标(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
Marker currentMarker=mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker,myMarker);
mMap.setInfoWindowAdapter(新的MarkerInfoWindowAdapter());
}
}
}
私有void setUpMap(){
//执行空检查以确认尚未实例化
//地图。
如果(mMap==null){
//尝试从SupportMapFragment获取映射。
mMap=((MapFragment)getFragmentManager().findFragmentById(
R.id.homemap().getMap();
车床坐标=新车床(纬度、经度);
CameraUpdate yourLocation=CameraUpdateFactory.newLatLngZoom(
协调,15);
mMap.animateCamera(您的位置);
//检查我们是否成功获得地图。
如果(mMap!=null){
setOnMarkerClickListener(新的GoogleMap.OnMarkerClickListener(){
@凌驾
公共布尔市场点击(
com.google.android.gms.maps.model.Marker){
marker.showInfoWindow();
返回true;
}
});
}否则
Toast.makeText(getActivity().getApplicationContext(),
“无法创建贴图”,Toast.LENGTH_SHORT.show();
}
}
公共类MarkerInfoWindowAdapter实现GoogleMap.InfoWindowAdapter{
public MarkerInfoWindowAdapter(){
}
@凌驾
公共视图getInfoWindow(标记器){
返回null;
}
@凌驾
公共视图getInfoContents(标记器){
视图v=getActivity().GetLayoutFlater().充气(
R.layout.infowindow_布局,空);
MyMarker MyMarker=mMarkersHashMap.get(marker);
ImageView标记图标=(ImageView)v.findViewById(R.id.marker_图标);
TextView标记标签=(TextView)v.findViewById(R.id.marker\u标签);
setImageResource(manageMarkerIcon(myMarker.getmIcon());
markerLabel.setText(myMarker.getmLabel());
TextView另一个标签=(TextView)v
.findViewById(R.id.另一个标签);
//另一个label.setText(“自定义文本”);
返回v;
}
}
私有int-manageMarkerIcon(字符串markerIcon){
if(markerIcon.equalsIgnoreCase(“3”))
返回R.可牵引救护车;
else if(markerIcon.equalsIgnoreCase(“4”))
返回R.drawable.doctor;
else if(markerIcon.equalsIgnoreCase(“me”))
返回R.drawable.marker;
其他的
返回R.drawable.marker;
}
}
我在下面附上我的logcat错误快照
这是xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".Googlemap.Googlemap">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
希望这能奏效。我搜索了很多,现在我找到了问题的解决方案 我在活动中编写以下代码
@Override
public void onDestroyView() {
try {
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.homemap);
if (getActivity().isFinishing()) {
}
else if (f != null)
getActivity().getFragmentManager().beginTransaction().remove(f)
.commit();
} catch (Exception e) {
}
super.onDestroyView();
}
感谢大家为您提供宝贵的时间和建议。从
Fragment
中删除MapFragment
。到目前为止,只有一种解决方法。从片段xmlns的xml代码中删除它:map=”“请给我代码,我需要在哪里删除它以及如何删除它?我还删除了MapFragment@Override public void onDestroyView(){super.onDestroyView();MapFragment f=(MapFragment)getFragmentManager()。findFragmentById(R.id.homemap);如果(f!=null)getFragmentManager().beginTransaction().remove(f).commit();}当我从另一个屏幕返回到此屏幕时,它对我有帮助,但当我从侧菜单栏选择调用同一屏幕时,应用程序崩溃。请建议我,谢谢回答。但如果您看到我的代码,我已经完成了相同的任务。我的问题不是显示地图。地图是在我第一次吃午饭时显示的,而是当用户从另一个屏幕返回时显示的即使在这个屏幕上,应用程序也会崩溃
@Override
public void onDestroyView() {
try {
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.homemap);
if (getActivity().isFinishing()) {
}
else if (f != null)
getActivity().getFragmentManager().beginTransaction().remove(f)
.commit();
} catch (Exception e) {
}
super.onDestroyView();
}