2014-01-11 170 views
1

我正在開發它採用片段標籤的應用程序,我的片段的人使用谷歌地圖V2Android的碎片谷歌地圖V2:錯誤充氣類片段

public class AppMapFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_map, container, false); 

    try { 
     // Loading map 
     initilizeMap(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    initializePOI(); 

    addMarkers(); 
    googleMap.setMyLocationEnabled(true); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria,true); 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location!=null) { 
      double latitude = location.getLatitude(); 
      double longitude = location.getLongitude(); 
      myPos = new LatLng (latitude, longitude); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPos, 15)); 
    } else { 
     // Move the camera instantly to London with a zoom of 15. 
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LONDON, 15)); 
    } 

    // Zoom in, animating the camera. 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

    return rootView; 
} 

我的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" 
android:background="#17df0d"> 

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

</RelativeLayout> 

我表現:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.networkme" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="18" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<!-- Required to show current location --> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<!-- Required OpenGL ES 2.0. for Maps V2 --> 
<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="example.networkme.activities.MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
    <activity 
     android:name="example.networkme.activities.LoginActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
    <activity 
     android:name="example.networkme.activities.SettingsActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
    <activity 
     android:name="example.networkme.activities.InstagramLoginActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 

    <activity 
     android:name="com.facebook.LoginActivity" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 

    <activity 
     android:name="example.networkme.activities.AppSettingsActivity" 
     android:label="@string/settings" /> 

    <meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/app_id" /> 

    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyC0LIYgIxPdlXkb9o7H5ppICC-qTUAykJE" /> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

</application> 

</manifest> 

錯誤:

FATAL EXCEPTION: main 
android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
at example.networkme.fragments.AppMapFragment.onCreateView(AppMapFragment.java:41) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 

我只複製錯誤的部分,因爲它很長:(。每當我嘗試運行應用程序時都會發生此錯誤。它引用的行是View rootView = inflater.inflate(R.layout.fragment_map, container, false);。任何幫助都感激不盡!在此先感謝

+1

我只是想知道你爲什麼要誇大一個MapFragmet(它已經是一個片段)另一個片段內! – yugidroid

+0

我的應用程序包含五個選項卡,其中一個是地圖。我認爲這樣做的方式是:( – Marcel

回答

1

看看這個實現,你應該爲你已經做了,創建一個片段,在它的地圖:

public class MapFragment extends Fragment 

創建佈局: mapview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:id="@+id/mapview" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
map:cameraZoom="12" 
map:mapType="normal" 
map:uiZoomControls="false" 
map:uiRotateGestures="true" 
map:uiScrollGestures="true" 
map:uiZoomGestures="true" 
map:uiTiltGestures="false" /> 

然後在該片段中做到這一點:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    return inflater.inflate(R.layout.mapview, container, false); 
} 

然後拿到地圖實例:

private GoogleMap getGoogleMap() { 
     if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) { 
      SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview); 
      if (smf != null) { 
       map = smf.getMap(); 
      } 
     } 
     return map; 
    }