我正在开发一个应用程序,使用
NavigationDrawer
(导航抽屉),即DrawerLayout
,并导航到不同的Fragments
。当我调用一个Map_Fragment_Page
时,应用程序会崩溃,但第一次不会。第一次它可以正确地显示Map
,但之后当我导航到不同的片段并再次返回Map_Fragment_Page
时,就会崩溃,并显示一个错误:android.view.InflateException: Binary XML file line #8: Error inflating class fragment
我尝试了许多不同的解决方案,也在Google
上搜索,但仍然没有得到所需的解决方案。问题还没有被解决。
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;
}
}
if(googleMap == null) { //your current code}
,而您没有指定if(googleMap != null) {marker = googleMap.addMarker(new MarkerOptions().position( new LatLng(latitude, longitude)).title("You are Here"));}
。请尝试像这样实现。 - user3110424