我正在开发使用 NavigationDrawer
即 DrawerLayout
一个应用程序,并导航到不同的片段
。当我称之为 Map_Fragment_Page
应用程序崩溃,但也不是第一次。这是第一次显示了地图
正常,但是当我浏览不同的片段,并再次来到 Map_Fragment_Page
之后则崩溃给了一个错误 android.view.InflateException:二进制XML文件中的行#8:错误充气类片段
我尝试了许多不同的解决方案,我也还上搜索谷歌
,但仍然没有得到所需要的解决方案。这个问题还没有固定的。
howtoreach.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<片段
机器人:ID =@ + ID / howtoreach_map
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.SupportMapFragment/>
< / RelativeLayout的>
HowToReach.java
包com.demo.map.howtoreach;
进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口android.support.v4.app.Fragment;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.MapFragment;
进口com.google.android.gms.maps.model.BitmapDescriptorFactory;
进口com.google.android.gms.maps.model.CameraPosition;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.Marker;
进口com.google.android.gms.maps.model.MarkerOptions;
进口com.google.android.gms.maps.model.Polyline;
进口com.google.android.gms.maps.model.PolylineOptions;
进口com.demo.map.R;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.graphics.Color;
进口android.location.Criteria;
进口android.location.Location;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Toast;
公共类HowToReach扩展片段
{
公共静态最终字符串变量=fragment_5;
ProgressDialog对话框;
GoogleMap的GoogleMap的;
标记标记;
LocationManager locationManager;
地点位置;
标准标准;
字符串提供商;
双纬度,经度;
公共HowToReach(){}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
最终的视图V = inflater.inflate(R.layout.howtoreach,集装箱,假);
对话框= ProgressDialog.show(getActivity(),,加载,真,假);
INT secondsDelayed = 4;
新的处理程序()。postDelayed(新的Runnable()
{
公共无效的run()
{
dialog.dismiss();
}
},secondsDelayed * 1000);
尝试
{
//载入地图
如果(GoogleMap的== NULL)
{
使用GoogleMap =((SupportMapFragment)getFragmentManager()findFragmentById(R.id.howtoreach_map)。)的GetMap()。
googleMap.setMyLocationEnabled(真正的);
locationManager =(LocationManager)getActivity()getSystemService(Context.LOCATION_SERVICE)。
标准=新标准();
供应商= locationManager.getBestProvider(标准,真正的);
位置= locationManager.getLastKnownLocation(供应商);
纬度= location.getLatitude();
经度= location.getLongitude();
//创建标记
标记= googleMap.addMarker(新MarkerOptions()。位置(
新的经纬度(纬度,经度))标题(你在这里))。
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
marker.showInfoWindow();
CameraPosition cameraPosition =新CameraPosition.Builder()。目标(
新的经纬度(纬度,经度))变焦(15).build()。
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
折线行= googleMap.addPolyline(新PolylineOptions()
。新增(新的经纬度(纬度,经度),新的经纬度(18.520897,73.772396))
.WIDTH(2)。颜色(Color.RED).geodesic(真));
标记= googleMap.addMarker(新MarkerOptions()。位置(
新的经纬度(18.520897,73.772396))标题(DSK Ranwara道))。
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
//如果映射成功与否创建检查
如果(GoogleMap的== NULL)
{
Toast.makeText(getActivity(),对不起,无法创建地图!,Toast.LENGTH_SHORT).show();
}
}
}
赶上(例外五)
{
e.printStackTrace();
}
返回伏;
}
}
解决方案
是的..我想映射一个片段里。
您应该使用图形页面
http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
公共类HowToReach扩展片段{
图形页面图形页面;
GoogleMap的地图;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.fragment2,集装箱,假);
//获取从XML布局的图形窗口和创建它
尝试 {
MapsInitializer.initialize(getActivity());
}赶上(GooglePlayServicesNotAvailableException E){
Log.e(地址映射,无法初始化谷歌玩,E);
}
开关(GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()))
{
案例ConnectionResult.SUCCESS:
Toast.makeText(getActivity(),成功,Toast.LENGTH_SHORT).show();
图形页面=(图形页面)v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
//获取从图形页面来使用GoogleMap并执行初始化的东西
如果(图形页面!= NULL)
{
地图= mapView.getMap();
map.getUiSettings()setMyLocationButtonEnabled(假)。
map.setMyLocationEnabled(真正的);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(新经纬度(43.1,-87.9),10);
map.animateCamera(cameraUpdate);
}
打破;
案例ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity()的服务缺失,Toast.LENGTH_SHORT).show();
打破;
案例ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(),需要更新,Toast.LENGTH_SHORT).show();
打破;
默认:Toast.makeText(getActivity(),GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()),Toast.LENGTH_SHORT).show();
}
//更新的MapView的位置和变焦
返回伏;
}
@覆盖
公共无效onResume(){
mapView.onResume();
super.onResume();
}
@覆盖
公共无效的onDestroy(){
super.onDestroy();
mapView.onDestroy();
}
@覆盖
公共无效onLowMemory(){
super.onLowMemory();
mapView.onLowMemory();
}
}
fragment2.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< com.google.android.gms.maps.MapView
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID /图/>
< / RelativeLayout的>
I am developing an app using a NavigationDrawer
i.e. DrawerLayout
and navigating to different Fragments
. When I call a Map_Fragment_Page
the application crashes, but not the first time. For the first time it displays the Map
properly but after that when I navigate different fragments and again come to Map_Fragment_Page
then it crashes giving an error android.view.InflateException: Binary XML file line #8: Error inflating class fragment
I tried so many different solutions and I also also searched on Google
but still not getting the required solution. The problem is not yet fixed.
howtoreach.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/howtoreach_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
HowToReach.java
package com.demo.map.howtoreach;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import android.support.v4.app.Fragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.demo.map.R;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class HowToReach extends Fragment
{
public static final String TAG = "fragment_5";
ProgressDialog dialog;
GoogleMap googleMap;
Marker marker;
LocationManager locationManager;
Location location;
Criteria criteria;
String provider;
double latitude, longitude;
public HowToReach(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.howtoreach, container, false);
dialog = ProgressDialog.show(getActivity(),"","Loading",true,false);
int secondsDelayed = 4;
new Handler().postDelayed(new Runnable()
{
public void run()
{
dialog.dismiss();
}
}, secondsDelayed * 1000);
try
{
// Loading map
if (googleMap == null)
{
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.howtoreach_map)).getMap();
googleMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
latitude = location.getLatitude();
longitude = location.getLongitude();
// create marker
marker = googleMap.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).title("You are Here"));
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
marker.showInfoWindow();
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(latitude, longitude), new LatLng(18.520897,73.772396))
.width(2).color(Color.RED).geodesic(true));
marker = googleMap.addMarker(new MarkerOptions().position(
new LatLng(18.520897, 73.772396)).title("DSK Ranwara Road"));
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// check if map is created successfully or not
if (googleMap == null)
{
Toast.makeText(getActivity(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return v;
}
}
解决方案
Yes.. I want Map inside a Fragment.
You should use a MapView
http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
public class HowToReach extends Fragment {
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
// Gets the MapView from the XML layout and creates it
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
Log.e("Address Map", "Could not initialize google play", e);
}
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
{
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if(mapView!=null)
{
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default: Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
// Updates the location and zoom of the MapView
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
fragment2.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">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</RelativeLayout>
上一篇:在Amazon OpsWorks使用Ruby 2.0OpsWorks、Amazon、Ruby
下一篇:Django的COM pressor使用gzip服务的JavaScriptCOM、Django、pressor、JavaScript