0

我有在抽屉布局的第二个菜单中使用地图的应用程序。当我试图用地图膨胀片段时,应用程序崩溃。Android:在抽屉布局中使用地图扩展片段时出错

以下是代码:

if (rootView != null) { 
     ViewGroup parent = (ViewGroup) rootView.getParent(); 
     if (parent != null) { 
      parent.removeView(rootView); 
     } 
    } 

    try { 
     rootView = inflater.inflate(R.layout.fragment_map, container, false); 


    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

以下是生成的异常:

android.view.InflateException:二进制XML文件行#23:错误充气类片段

我已经尝试了很多堆栈和其他平台上的解决方案,但徒劳无功。

这是怎么了我在我的XML使用父母的RelativeLayout内地图片段:

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.SupportMapFragment"/> 

我也试过在其onDestroyView methos取出地图片段:

@Override 
public void onDestroyView() { 

    super.onDestroyView(); 

    SupportMapFragment fragment = ((SupportMapFragment) getChildFragmentManager() 
      .findFragmentById(R.id.map)); 
    try{ 
     if (fragment != null) 
      getChildFragmentManager().beginTransaction().remove(fragment).commit(); 
    }catch(Exception e){ 
    } 


} 

以下是清单文件数据:

<meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <uses-library android:name="com.google.android.maps" /> 
    <activity 
     android:name="com.app.myapp.SplashActivity" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
     android:label="@string/app_name" 
     android:noHistory="true" 
     android:screenOrientation="portrait" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name="com.app.myapp.LoginActivity" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
       android:label="@string/app_name" 
       android:launchMode="singleTop" 
       android:screenOrientation="portrait" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
       > 
    </activity> 

    <activity 
     android:name="com.app.myapp.MainActivity" 
     android:launchMode="singleTop" 
     android:screenOrientation="portrait" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:label="@string/app_name" > 

    </activity> 

+0

按我的方式,你应该从'片段Transaction'删除'地图Fragment'当你去到另一个'片段'或屏幕。 –

+0

是的,我已经在OnDestroyView方法中完成了它,但它没有解决我的问题。问题是带有地图的片段在首次启动时崩溃。 –

+0

如何?发布你的代码...也发布你的清单 –

回答

0

我们不能在扩展片段A类膨胀静态片段。相反,您可以通过下面的代码动态替换/添加片段(MapFragment)。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" 
     android:layout_margin="0dp" 
     android:layout_weight="2" 
     android:background="@android:color/transparent" > 

     <FrameLayout 
      android:id="@+id/fragContainer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/top_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <EditText 
      android:id="@+id/inputSearch" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@android:drawable/ic_menu_search" 
      android:hint="Search Tag" 
      android:inputType="text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom" 
     android:layout_margin="0dp" 
     android:layout_weight="2" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <ProgressBar 
       android:id="@+id/progressBar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical|center_horizontal" 
       android:indeterminateDrawable="@drawable/my_progress_indeterminate" 
       android:visibility="gone" > 
      </ProgressBar> 

      <ListView 
       android:id="@+id/crowdmap_list" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:drawSelectorOnTop="false" 
       android:listSelector="@drawable/abc_list_selector_holo_dark" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

</RelativeLayout> 

,并通过代码现在你可以添加/更换您的mapframent到的FrameLayout像下面

FragmentManager fm = getChildFragmentManager(); 
fm.beginTransaction().replace(R.id.fragContainer, SupportMapFragment.newInstance(), "map").commit(); 
相关问题