2013-01-01 51 views
2

新年快樂所有,安卓SupportMapFragment錯誤膨脹片段類

新與Android中實現片段(我已經看到了類似的帖子,但我一直沒能得到我的問題的答案),所以這裏有雲:

我希望用SupportMapFragment(從支持庫V4引用),我創建了一個類,它擴展FragmentActivity,叫TripSummary.java:

package com.project.locationapp; 

import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import android.os.Bundle; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

public class TripSummary extends android.support.v4.app.FragmentActivity { 

private GoogleMap mMap; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = new Intent(this, LocationService.class); 
    stopService(intent); 
    setContentView(R.layout.activity_trip_summary); 

    createMap(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_trip_summary, menu); 
    return true; 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    //Content? 
    return super.onOptionsItemSelected(item); 
} 

private void createMap(){ 
    //if a map has not already been instantiated it'll return null 
    if(mMap == null){ 
     //instantiate map 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     //check it has been instantiated 
     if(mMap != null){ 
      Toast.makeText(this, "map done", Toast.LENGTH_SHORT) 
      .show(); 
      //Manipulate map here (add coordinates/polylines from trip etc etc.) 
     } 
    } 
} 

}

從activity_trip_summary.xml得到一個佈局

<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" > 

<TextView 
    android:id="@+id/tripSummary" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="@string/trip_summary" 
    tools:context=".Start" /> 

<TextView 
    android:id="@+id/tripDetails" 
    android:layout_below="@id/tripSummary" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="@string/awaiting_location" 
    tools:context=".Start" /> 

<Fragment 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/map" 
    android:layout_below="@id/tripDetails" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.SupportMapFragment" /> 

</RelativeLayout> 

的AndroidManifest.xml如下:

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 
<permission 
     android:name="com.project.locationapp.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
<uses-permission android:name="com.project.locationapp..permission.MAPS_RECEIVE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<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_FINE_LOCATION" /> 
<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 


<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name="com.project.locationapp.Start" 
     android:label="@string/title_activity_start" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <service android:name="com.project.locationapp.LocationService" /> 

    <activity 
     android:name="com.project.locationapp.TripSummary" 
     android:label="@string/title_activity_trip_summary" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.project.locationapp.Start" /> 
    </activity> 
    <activity 
     android:name="com.project.locationapp.ViewTrips" 
     android:label="@string/title_activity_view_trips" > 
    </activity> 
    <activity 
     android:name="com.project.locationapp.TripDetail" 
     android:label="@string/title_activity_trip_detail" > 
    </activity> 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="<-- My Key -->"/> 
</application> 

</manifest> 

我有在每一次我嘗試加載活動TripSummary的問題,我得到一個'對不起!意外停止「對話框並且應用程序崩潰。

的logcat:

java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.project.locationapp/com.project.locationapp.TripSummary}:  android.view.InflateException: Binary XML file line #24: Error inflating class Fragment 

我引用了谷歌,播放services_lib庫(這也是我的工作區)和中我也有Android的支持,v4.jar在/ libs目錄副本我的項目。

如果您知道我的問題的答案,請分享!

在此先感謝。

回答

1

請更換:

<Fragment 

有:

<fragment 

此外,您還可以擺脫在該元素的冗餘/不正確的名稱空間聲明。

此外,在未來,可以發佈完整的堆棧跟蹤,而不僅僅是一行的一部分,以方便人們幫助您。

+0

謝謝。 (菜鳥還是什麼?) – user1610177

+0

@ user1610177:你不是第一個被「」區分大小寫的人絆倒的人。一般來說,Android中的大多數元素名稱都區分大小寫。我還提交了一項功能請求,讓這些工具警告開發人員嘗試使用'':http://code.google.com/p/android/issues/detail?id=42173 – CommonsWare

+0

@CommonsWare Google地圖API看起來像是完全瘋狂。自從過去30天以來,我收到了「錯誤膨脹佈局」問題。請幫幫我。嘗試了所有可用的修復。如果你想要,可以另外提出一個問題。 – 10101010