0

Hy guys,SupportMapFragment XML錯誤

我試圖使用SupportMapFragment將Google Maps API加載到我的應用程序中。會發生什麼是我嘗試使用特定的佈局,而不是簡單的地圖佔用整個屏幕。

這裏是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/greenish" 
    android:orientation="vertical" 
    android:id="@+id/location_stuff" > 

    <RelativeLayout 
     android:id="@+id/location_header" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.7" 
     android:background="@color/greenish" > 

     <RelativeLayout 
      android:id="@+id/cancel_btn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="20dp" 
      android:background="@drawable/done_rounded_btn" 
      android:gravity="center" > 

      <TextView 
       android:id="@+id/retake_txt" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:layout_marginTop="10dp" 
       android:gravity="center" 
       android:paddingLeft="7dp" 
       android:paddingRight="7dp" 
       android:text="@string/cancel" 
       android:textAllCaps="true" 
       android:textColor="#FFFFFF" 
       android:textSize="16sp" /> 
     </RelativeLayout> 

     <TextView 
      android:id="@+id/location_txt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:gravity="center_vertical" 
      android:text="@string/location_map" 
      android:textAllCaps="true" 
      android:textColor="@color/done_color" 
      android:textSize="16sp" /> 

    </RelativeLayout> 

    <fragment 
     android:id="@+id/mapview" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="5.6" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     class="com.google.android.gms.maps.LocationFragment" /> 

    <RelativeLayout 
     android:id="@+id/save_location_layout" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/greenish" > 

     <LinearLayout 
      android:id="@+id/delete_btn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="25dp" 
      android:layout_marginRight="25dp" 
      android:layout_centerVertical="true" 
      android:background="@drawable/done_rounded_btn" 
      android:orientation="horizontal" 
      android:paddingBottom="7dip" 
      android:paddingTop="7dip" 
      android:weightSum="7" > 

      <TextView 
       android:id="@+id/delete_txt" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="6" 
       android:background="@null" 
       android:paddingLeft="10dp" 
       android:text="@string/save_location" 
       android:textColor="@color/white" 
       android:textAllCaps="true" 
       android:textSize="18sp" /> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="1" 
       android:contentDescription="@string/hello_world" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:src="@drawable/input_map_white" /> 
     </LinearLayout> 
    </RelativeLayout> 

</LinearLayout> 

我已經通過擴展創建我的具體SupportMapFragment類是這樣的:

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.captoom.R; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 

public class LocationFragment extends SupportMapFragment { 

    private GoogleMap map=null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.preview_location, container, false); 

     SupportMapFragment fragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapview); 
     setMapp(fragment.getMap()); 
     return v; 
    } 

    public GoogleMap getMapp() { 
     return map; 
    } 

    public void setMapp(GoogleMap map) { 
     this.map = map; 
    } 
} 

當我嘗試將片段添加到我的當前活動我得到一個XML膨脹錯誤。

我嘗試添加該片段是這樣的:

LocationFragment mMapFragment = new LocationFragment(); 
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
fragmentTransaction.add(mMapFragment,null); 
fragmentTransaction.commit(); 

以下是錯誤日誌:

07-30 17:57:24.144: E/AndroidRuntime(8231): FATAL EXCEPTION: main 
07-30 17:57:24.144: E/AndroidRuntime(8231): android.view.InflateException: Binary XML file line #55: Error inflating class fragment 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at ro.gebs.captoom.fragments.LocationFragment.onCreateView(LocationFragment.java:19) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:299) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at ro.gebs.captoom.fragments.LocationFragment.onCreateView(LocationFragment.java:19) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.os.Handler.handleCallback(Handler.java:615) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.os.Looper.loop(Looper.java:137) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.app.ActivityThread.main(ActivityThread.java:4921) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at dalvik.system.NativeStart.main(Native Method) 
07-30 17:57:24.144: E/AndroidRuntime(8231): Caused by: java.lang.IllegalArgumentException: Binary XML file line #55: Duplicate id 0x7f06009a, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.LocationFragment 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 
07-30 17:57:24.144: E/AndroidRuntime(8231):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682) 

任何形式的幫助高度讚賞。

謝謝!

+0

請您能不能告訴你,幫助我們來幫助你的錯誤? – AndroidDev

+0

我使用錯誤日誌更新了我的帖子 –

+0

您的Android構建目標是Google API平臺嗎? – AndroidDev

回答

2

如果您在onDestroyView()中刪除所有嵌套片段,則可以解決此問題。

public void onDestroyView() { 
     super.onDestroyView(); 
     Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
     FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
     ft.remove(fragment); 
     ft.commit(); 
} 

而且在onCreateView誇大他們像往常一樣()

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.map, container, false); 
} 
+0

感謝您的解決方案! –

+0

很高興我幫助:D – AndroidDev

+0

這是不工作時分離/附加片段.. –