2016-07-12 34 views
0

我有一個基於NavigationDrawer的應用程序。我加載的片段之一包含GoogleMap片段。地圖片段在使用後退按鈕輸入時崩潰

這是什麼,是用來加載片段

getSupportFragmentManager().beginTransaction().replace(R.id.container, 
    new MyMapFragment()).addToBackStack("mytag1").commit(); 

該頁面加載,沒有任何問題,並顯示map.But然後當我用菜單切換到另一個片段,然後按返回鍵,應用程序崩潰。該片段僅包含加載地圖的最少代碼。

片段XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/ll_report_in_out" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:orientation="vertical" 
    tools:context="my.package.name" > 

     <LinearLayout 
      android:id="@+id/llv_layer2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" 
     > 
      <!-- some content --> 
     </LinearLayout> 

     <fragment 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      class="com.google.android.gms.maps.SupportMapFragment" 
      android:name="my.package.name.MyMapFragment" 
     /> 
</LinearLayout> 

活動:

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

     try { 
      fm = ((SupportMapFragment) getChildFragmentManager() 
        .findFragmentById(R.id.map)); 

      if (fm != null) 
       map = fm.getMap(); 
      else 
       map = null; 
     } 
     catch(Exception e) 
     { Log.i("dbg",e.toString()); }       
} 

漁獲以上不火。 Logcat在線125上說膨脹異常,這是放置地圖片斷的地方。

我真的很感激任何幫助......

我加入我目前的測試條件和工作環境的情況下,它的事項

AndroidStudio 2.1 敏SDK版本10 目標SDK版本24 編譯SDK版本24

測試Nexus 6上運行的是Android 6.0.1

+1

回覆on Fragment時會調用'onCreateView'方法嗎? –

+0

@языкK是當我按下後退按鈕時,再次調用onCreateView。 – Deepak

回答

0

嘗試直接使用MapViewFragment xml文件中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:scrolling_image_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".screens.DashBoardFragment"> 
    <!-- Component 1 to cover half screen height --> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.50" 
     android:layout_gravity="top" 
     > 

     <EditText android:id="@+id/url_live_target" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/textSizeNormal" 
       android:inputType="textUri" 
       android:singleLine="true" 
       android:hint="Receive Live Update URL" /> 
    </FrameLayout> 

    <!-- Component 2 to cover half screen height --> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.50" 
     > 

     <com.google.android.gms.maps.MapView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/map_dashBoard" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="bottom" 
      /> 
      </FrameLayout> 
</LinearLayout> 

現在您的片段

private MapView mMapView; 
    private GoogleMap mGoogleMap; 

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

     mMapView = (MapView) view.findViewById(R.id.map_dashBoard); 
     mMapView.onResume(); 

     try { 
      MapsInitializer.initialize(getActivity().getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     mGoogleMap = mMapView.getMap(); 

    } 

您可能需要根據您的具體要求編輯邏輯。 [片段添加/替換等]

+0

我無法做到這一點,你可以從佈局中知道,我需要在地圖上方顯示一些內容。 – Deepak

+0

即使當我加載包含地圖視圖的片段時,您的代碼段也會崩潰,並出現以下錯誤嘗試調用空對象引用上的接口方法'void maps.af.y.o()'。 – Deepak

+0

@Deepak:我可以詳細說明嗎? 「void maps.af.y.o()」 – Stallion

0

試試這個代碼。這解決了我的問題

View view = null; 

在onCreateView()檢查此條件

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
if(view != null) 
{ 
    ViewGroup parent = (ViewGroup) container; 
    if(parent !=null) 
    { 
    parent.removeView(view); 
    } 
} 
try 
{ 
    view = inflater.inflate(R.layout.fragment_report_in_out, container, false); 
} 
catch(Exception e){} 
return view; 
} 

希望這有助於!

相關問題