Android片段 - 应用程序在关闭应用程序后崩溃

时间:2014-06-12 09:49:56

标签: android android-fragments android-mapview android-fragmentactivity

在我的应用程序中,我在fragment使用谷歌地图。我正在使用此代码添加fragment s。

 public void addPage(final BaseFragment pBaseFragment, final boolean isAddToBackStack){
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.add(R.id.content_frame, pBaseFragment);
            if (isAddToBackStack) transaction.addToBackStack(null);
            transaction.commit();  
    }

MyMapFragment中我将其称为删除地图(如果我不这样做,我会得到地图例外的重复ID)

   public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_n));
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

但是当我按下后退按钮并且应用程序关闭时,我收到了此错误。 (在ft.commit()行中

 Caused by: java.lang.IllegalStateException: Activity has been destroyed
            at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
            at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
            at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)

有谁能告诉我这里有什么解决方案如何避免这个错误我已经停留在这3天了。

1 个答案:

答案 0 :(得分:3)

尝试这种方式:

   @Override
    public void onDestroyView ()
    {
        try{
          SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_n));
          FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
          ft.remove(fragment);
          ft.commit();
        }catch(Exception e){
        }
      super.onDestroyView();  
  }