我正在一個項目中有很多片段連接到一個片段管理器。我想在我的片段活動中實現onbackpressed方法,以便我可以回到以前的片段。使用onbackpressed()時使用片段包含地圖片段
的onbackpressedcode如下所示:
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getSupportFragmentManager().popBackStackImmediate();
}
}
的問題是,有在fragment.There之一supportmapfragment在該片段列表視圖。當我需要我的列表視圖中單擊某個項到包含details.When另一個片段我按回到我得到一個IllegalStateException如下圖所示:
10-27 13:31:26.039: E/AndroidRuntime(15458): FATAL EXCEPTION: main
10-27 13:31:26.039: E/AndroidRuntime(15458): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-27 13:31:26.039: E/AndroidRuntime(15458): at com.igloo.fragments.ViewjobsFragment.onCreateView(ViewjobsFragment.java:123)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1527)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:494)
10-27 13:31:26.039: E/AndroidRuntime(15458): at com.igloo.marinaretail.UserMain.onBackPressed(UserMain.java:476)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.app.Activity.onKeyUp(Activity.java:1898)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
10-27 13:31:26.039: E/AndroidRuntime(15458): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1705)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2626)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2601)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.ViewRoot.handleMessage(ViewRoot.java:1873)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.os.Looper.loop(Looper.java:130)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.app.ActivityThread.main(ActivityThread.java:3689)
10-27 13:31:26.039: E/AndroidRuntime(15458): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 13:31:26.039: E/AndroidRuntime(15458): at java.lang.reflect.Method.invoke(Method.java:507)
10-27 13:31:26.039: E/AndroidRuntime(15458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-27 13:31:26.039: E/AndroidRuntime(15458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-27 13:31:26.039: E/AndroidRuntime(15458): at dalvik.system.NativeStart.main(Native Method)
10-27 13:31:26.039: E/AndroidRuntime(15458): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f06005e, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
10-27 13:31:26.039: E/AndroidRuntime(15458): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-27 13:31:26.039: E/AndroidRuntime(15458): ... 26 more
如何防止上述問題的緊迫回來。
此外,在包含地圖,我有下面的代碼片段的方法的OnDestroy:
@Override
public void onDestroy() {
SupportMapFragment f = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
if (f.isResumed()){
getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
}
super.onDestroy();
}
如何解決回壓問題?
有沒有辦法檢查堆棧中的前一個片段是否是包含supportmap的片段?
試試這個幫助我http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin/41381507#41381507 – 2016-12-29 14:24:07