我的应用程序在抽屉布局的第二个菜单中有地图。当我尝试使用地图为片段充气时,应用程序崩溃。
以下是代码:
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>