因此,我有一个包含标签的活动,每个标签都有一个分配给它的片段,因此您可以使用标签浏览片段。 在其中两个选项卡中,我必须加载一个地图(每个地图上有不同的信息,不同的引脚等)问题是它只加载第一个,我使用这个代码为每个标签分配所需的片段(这个是一个工作正常的适配器。)
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return new SearchFragment();
case 1:
return new ParkFragment();
case 2:
setActiveSessionsFragment();
return mActiveSessionsFragment;
case 3:
return new ChoosePaymentMethodFragment();
default:
return new ParkFragment();
}
}
是0和2加载地图的情况,如果我将其中任何一个替换为任何其他片段,那么带有地图的那个片段工作正常,但是如果让两个应用程序崩溃。在这两个片段中,我正在像这样的一个FrameLayout中加载地图:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/map_container_search_tab"
android:background="@color/gray"></FrameLayout>
我通过以下方式以编程方式创建它们:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Create a new map fragment
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
getFragmentManager().beginTransaction().add(R.id.map_container_search_tab, mapFragment).commit();
mapFragment.getMapAsync(this);
}
但是我没有onDestroy方法也没有onDestroyView,我试图通过我在其他响应中看到的来实现这些但是它没有用,我试着自己解决这个问题,就像整整一周一样。这是我试过但没有运气的方法
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
getFragmentManager().beginTransaction()
.remove(getFragmentManager().findFragmentById(R.id.map_container))
.commit();
}
崩溃时这是logcat:
05-16 15:09:33.540 24971-24971/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parso.mobile.android, PID: 24971
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1507)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at com.parso.mobile.android.fragments.SearchFragment.onActivityCreated(SearchFragment.java:108)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:946)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2910)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
at com.parso.mobile.android.MainActivity.onParkingSessionCreated(MainActivity.java:240)
at com.parso.mobile.android.fragments.ConfirmSessionFragment$CreateParkingSessionTask.onPostExecute(ConfirmSessionFragment.java:213)
at com.parso.mobile.android.fragments.ConfirmSessionFragment$CreateParkingSessionTask.onPostExecute(ConfirmSessionFragment.java:189)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)