2016-11-29 17 views
0

我正在製作一個應用程序,並且我只使用單個活動和更多片段,並且當我要將其中一個片段放到另一個片段中時,我有第一次單擊它時列表和每個項目都有地圖按鈕,它是開放地圖,但當我們再次點擊地圖按鈕時,它會給出gsm碎片膨脹錯誤。 我不知道爲什麼它在第一次在關閉地圖之後創建地圖時發生這個錯誤,我們再次打開時會出現錯誤。片段列表中的地圖

imageButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog = new Dialog(getActivity()); 
        dialog.setContentView(R.layout.dialog_map); 
        dialog.setTitle("Map"); 

        dialog.show(); 
        Button closeButton = (Button) dialog.findViewById(R.id.mr_close); 
        final LinearLayout frg=(LinearLayout)dialog.findViewById(R.id.frg); 
        closeButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          frg.removeAllViews(); 
          dialog.dismiss(); 
         } 
        }); 
       } 
      }); 


<LinearLayout android:id="@+id/frg" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".8"/> 

    <Button android:id="@+id/mr_close" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 

      android:layout_weight=".2" 
      android:text="close"/> 
</LinearLayout> 
+0

請補充完整代碼的理解 –

+0

你應該通過任何你想用的參數映射到片段上顯示的經緯度,然後向他們展示在地圖上 –

回答

0

這是由於重複的片段,刪除它closeButton點擊

try { 
     android.app.Fragment fragment = getFragmentManager().findFragmentById(R.id.map); 
     if (fragment != null) { 
      android.app.FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.remove(fragment); 
      ft.commit(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
0

你當你的ListFragment已刪除從充氣列表項中刪除地圖。您也可以查看您的地圖是否充氣之前在列表片段onCreate和刪除地圖之前充氣列表片段佈局。

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    SupportMapFragment supportMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)); 
    if (supportMapFragment != null) 
     getActivity().getSupportFragmentManager().beginTransaction().remove(supportMapFragment).commit(); 
} 

刪除地圖,同時破壞片段。像,

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    SupportMapFragment supportMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)); 
    if (supportMapFragment != null && getActivity() != null) { 
     try { 
      getActivity().getSupportFragmentManager().beginTransaction().remove(supportMapFragment).commitAllowingStateLoss(); 
     } catch (Exception e) { 

     } 
    }