【发布时间】:2015-07-17 12:01:56
【问题描述】:
我的应用程序在抽屉布局的第二个菜单中有地图。当我尝试使用地图为片段充气时,应用程序崩溃。
以下是代码:
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>
【问题讨论】:
-
根据我的观点,当您转到另一个
Fragment或屏幕时,您应该从Fragment Transaction中删除map Fragment。 -
是的,我已经在 OnDestroyView 方法中完成了它,但它并没有解决我的问题。问题是带有地图的片段在首次启动时崩溃。
-
怎么样???发布您的代码...同时发布您的清单
-
你为什么使用
getChildFragmentManager()?以及你是如何初始化地图的? -
这就是我初始化地图的方式: mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();但问题是应用程序在此行之前崩溃。当我尝试在我的地图片段中放大视图时,它会崩溃。
标签: android android-layout android-fragments android-side-navigation