使用Android Fragment膨胀异常

时间:2015-02-13 08:28:49

标签: android android-fragments

我需要在Android应用中使用Google地图。我添加了Google Play服务,并使用Google API控制台创建了SH1和API密钥。在我的Android应用中当我在.Xml中使用Fragment时,我收到的错误是Android.view .inflateException.Please帮我解决这个问题。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

       int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        if(status == ConnectionResult.SUCCESS) {
        Toast.makeText(getApplicationContext(), "GOOGLE PLAY SERVICE AVAILABLE", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "GOOGLE PLAY SERVICE AVAILABLE", Toast.LENGTH_LONG).show();
    }

    try { 
        if (googleMap == null) {
           googleMap = ((MapFragment) getFragmentManager().
           findFragmentById(R.id.the_map)).getMap();
        }
     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


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

}

main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
map:cameraTilt="45"
map:cameraZoom="14"
/>

的AndroidManifest.xml:          

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission      android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="com.example.maps.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
     <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBSQwglzqj0xivCRkQ0rhxi6BiKslrbFvA" />
</application>


</manifest>

1 个答案:

答案 0 :(得分:0)

使用Google Maps v2并在布局中添加

<fragment
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

获取更多帮助Google Maps V2