2014-01-05 78 views
1

我有一個主類繼承FragmentActivty和CustomMapFragment,它擴展了SupportMapFragment,當我將這個customMapFragment調用到FragmentActivity中時,我在logCat中看到這個錯誤。SupportMapFragment - 二進制xml文件行#2:錯誤充氣類片段

logcat的結果:

01-05 17:43:13.916: D/ERROR(2371): Binary XML file line #2: Error inflating class fragment 
01-05 17:43:13.936: W/System.err(2371): android.view.InflateException: Binary XML file line 2: Error inflating class fragment 
01-05 17:43:13.936: W/System.err(2371):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
01-05 17:43:13.946: W/System.err(2371):  at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
01-05 17:43:13.958: W/System.err(2371):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
01-05 17:43:13.958: W/System.err(2371):  at com.main.whereismyfriend.CustomMapFragment .onCreateView(CustomMapFragment .java:48) 
01-05 17:43:13.958: W/System.err(2371):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
01-05 17:43:13.958: W/System.err(2371):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
01-05 17:43:13.966: W/System.err(2371):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
01-05 17:43:13.966: W/System.err(2371):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
01-05 17:43:13.966: W/System.err(2371):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 
01-05 17:43:13.976: W/System.err(2371):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 
01-05 17:43:14.006: W/System.err(2371):  at android.os.Handler.handleCallback(Handler.java:730) 
01-05 17:43:14.006: W/System.err(2371):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-05 17:43:14.016: W/System.err(2371):  at android.os.Looper.loop(Looper.java:137) 
01-05 17:43:14.035: W/System.err(2371):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
01-05 17:43:14.035: W/System.err(2371):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-05 17:43:14.056: W/System.err(2371):  at java.lang.reflect.Method.invoke(Method.java:525) 
01-05 17:43:14.066: W/System.err(2371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
01-05 17:43:14.075: W/System.err(2371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-05 17:43:14.075: W/System.err(2371):  at dalvik.system.NativeStart.main(Native Method) 
01-05 17:43:14.096: W/System.err(2371): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f050011, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment 
01-05 17:43:14.156: W/System.err(2371):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290) 
01-05 17:43:14.166: W/System.err(2371):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
01-05 17:43:14.166: W/System.err(2371):  ... 18 more 
01-05 17:43:14.186: D/AndroidRuntime(2371): Shutting down VM 
01-05 17:43:14.186: W/dalvikvm(2371): threadid=1: thread exiting with uncaught exception (group=0x41465700) 
01-05 17:43:14.256: E/AndroidRuntime(2371): FATAL EXCEPTION: main 
01-05 17:43:14.256: E/AndroidRuntime(2371): java.lang.NullPointerException 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at com.main.whereismyfriend.CustomMapFragment .onResume(CustomMapFragment .java:96) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.Fragment.performResume(Fragment.java:1521) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.os.Handler.handleCallback(Handler.java:730) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.os.Looper.loop(Looper.java:137) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at java.lang.reflect.Method.invoke(Method.java:525) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-05 17:43:14.256: E/AndroidRuntime(2371):  at dalvik.system.NativeStart.main(Native Method) 

MainActivity:

public class MainActivity extends FragmentActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

      ... 

    CustomMapFragment fragment = new CustomMapFragment (getApplicationContext(),this); 
     getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit(); 

      ...  
} 

CustomMapFragment:

public class CustomMapFragment extends SupportMapFragment implements ConnectionCallbacks,OnConnectionFailedListener,LocationListener { 

private LocationClient locationClient; 
private LocationRequest locationRequest; 
private GoogleMap map; 
FragmentActivity activity; 
private Context context; 
View view; 

public CustomMapFragment (Context context,FragmentActivity activity){ 
    this.context = context; 
    this.activity = activity; 
} 

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

    super.onCreateView(inflater, container, savedInstanceState); 

    try { 
     view = inflater.inflate(R.layout.map_layout, container,false); 


     SupportMapFragment suppFragManager = (SupportMapFragment)activity.getSupportFragmentManager().findFragmentById(R.id.map); 
     map = suppFragManager.getMap(); 

     locationClient = new LocationClient(context,this,this); 

     locationRequest = LocationRequest.create(); 
     locationRequest.setInterval(9000); 
     locationRequest.setFastestInterval(900); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

     return view; 
    } catch (Exception e) { 
     Log.d("ERROR", e.getMessage()); 
     e.printStackTrace(); 
    } 

    return null; 

} 

... 

map_layout:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

的AndroidManifest.xml:

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


<permission 
    android:name="com.main.whereismyfriend.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/> 
<uses-permission android:name="com.main.whereismyfriend.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.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/>  

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name"> 

    <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="***" /> 

    ... 

</application> 

注:我用android.support.v4 LIB在這個類

回答

8
public class CustomMapFragment extends SupportMapFragment 

所以,CustomMapFragmentSupportMapFragment ...

view = inflater.inflate(R.layout.map_layout, container,false); 

。 ..其中你試圖膨脹另一個SupportMapFragment

這將無法正常工作。

要麼已經CustomMapFragment延長SupportMapFragment(並刪除其onCreateView()map_layout.xml使用map_layout.xml。不要這樣做。

0

在你的情況下,一切是完美的只是檢查是否在清單文件

添加了兩個關鍵如果一切沃金同谷歌的代碼,那麼請查一下我的情況下,清單文件我添加地理鍵和地圖密鑰,該密鑰爲什麼發生異常時,

注意 - 不要在清單文件中刪除地圖項中添加兩個鍵

meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="@string/google_maps_key"/> 

以上代碼,並且添加此代碼。

<meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/auto_location"/> 

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