0

我的应用程序在抽屉布局的第二个菜单中有地图。当我尝试使用地图为片段充气时,应用程序崩溃。

以下是代码:

 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: Binary XML file line #23: Error inflating class fragment

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

这就是我在 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

@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>

4

1 回答 1

0

我们不能在扩展 Fragment 的类中膨胀静态片段。相反,您可以通过如下代码动态替换/添加片段(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();
于 2015-05-07T05:46:18.203 回答