2016-07-12 38 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; 
} 

希望这有助于!

相关问题