我正在使用Google map api v2,在片段中成功集成地图,但在点击后退按钮时收到错误。
我有2个片段,即1. MapFragment 2. GalleryFragment,当用户导航到MapFragment然后从导航器抽屉移动到GalleryFragment然后按下android后退按钮然后app force关闭并显示错误 -
12-17 12:45:16.773: E/AndroidRuntime(15912): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the childs parent first.
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewGroup.addView(ViewGroup.java:1871)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewGroup.addView(ViewGroup.java:1828)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewGroup.addView(ViewGroup.java:1808)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1516)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:488)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:179)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.support.v7.app.ActionBarActivity.onBackPressed(ActionBarActivity.java:250)
12-17 12:45:16.773: E/AndroidRuntime(15912): at com.pehel.restaurant.MainActivity.onBackPressed(MainActivity.java:174)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.app.Activity.onKeyUp(Activity.java:1901)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.KeyEvent.dispatch(KeyEvent.java:1295)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.app.Activity.dispatchKeyEvent(Activity.java:2081)
12-17 12:45:16.773: E/AndroidRuntime(15912): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1785)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2650)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2625)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.view.ViewRoot.handleMessage(ViewRoot.java:1875)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.os.Looper.loop(Looper.java:130)
12-17 12:45:16.773: E/AndroidRuntime(15912): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-17 12:45:16.773: E/AndroidRuntime(15912): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:45:16.773: E/AndroidRuntime(15912): at java.lang.reflect.Method.invoke(Method.java:507)
12-17 12:45:16.773: E/AndroidRuntime(15912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-17 12:45:16.773: E/AndroidRuntime(15912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-17 12:45:16.773: E/AndroidRuntime(15912): at dalvik.system.NativeStart.main(Native Method)
我在mapFragment的onCreateView中使用代码:
if(v!=null){
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
} else {
v = inflater.inflate(R.layout.locate_us_fragment, container, false);
}
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
和onDestroy:
SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
if(fragment!=null) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
如何解决此问题,提前谢谢。
答案 0 :(得分:2)
我已经创建了一个库,以便更容易地使用mapFragment - 你可以看看它here
但是为了快速回答,我遇到了同样的问题,所以我写了删除地图片段方法(提到的@VenomVendor)到onSaveInstanceState部分。
答案 1 :(得分:1)
更新:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
setUpMapIfNeeded();
} catch (InflateException e) {
Log.wtf("*****", e.getMessage());
}
return view;
}
你必须杀死这个片段。
@Override
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.mapd));
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}