2014-02-05 89 views
3

我使用這種方法後:Separate Back Stack for each tab in Android using Fragments「java.lang.IllegalStateException:活動遭到破壞」恢復FragmentActivity在Android

我想從第一片段推片段,恢復FragmentActivity此操作導致後「活動已被摧毀」的錯誤。當第一次推片段時它工作正常,但當我暫停(最小化應用程序)該FragmentActivity時,重新打開它,然後嘗試推片段,它會給出錯誤。我知道這個問題已經被問過很多次了,但是我無法從中得到任何解決方案。

錯誤

02-05 11:51:01.889: E/AndroidRuntime(379): FATAL EXCEPTION: main 
02-05 11:51:01.889: E/AndroidRuntime(379): java.lang.IllegalStateException: Activity has been destroyed 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at com.app.main.AppMainTabActivity.pushFragments(AppMainTabActivity.java:650) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at com.app.home.Home$ExpandableListAdapter$1.onClick(Home.java:530) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.view.View.performClick(View.java:2408) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.view.View$PerformClick.run(View.java:8816) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.os.Handler.handleCallback(Handler.java:587) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.os.Looper.loop(Looper.java:123) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-05 11:51:01.889: E/AndroidRuntime(379):  at dalvik.system.NativeStart.main(Native Method) 

我點擊這推動第二片的第一片段可擴展列表視圖項。我在BaseExpandableListAdapter中顯示錯誤。在AppMainFragmentActivity

Push和Pop方法

public void pushFragments(String tag, Fragment fragment, boolean shouldAnimate, boolean shouldAdd) { 
    if (shouldAdd) { 
     mStacks.get(tag).push(fragment); 
    } 

    if (!isFinishing()) { 
     FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction ft = manager.beginTransaction(); 
     if (shouldAnimate) 
      ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left); 
     ft.replace(R.id.realtabcontent, fragment); 
     ft.commit(); 
     } 
    else { 
     FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction ft = manager.beginTransaction(); 
     if (shouldAnimate) 
      ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left); 
     ft.replace(R.id.realtabcontent, fragment); 
     ft.addToBackStack(null); 
     ft.commitAllowingStateLoss();//error shows here also. 
    } 
} 

public void popFragments() { 
    /*if (!isFinishing()) { }*/ 
    Fragment fragment = mStacks.get(mCurrentTab).elementAt(mStacks.get(mCurrentTab).size() - 2); 
    /*pop current fragment from stack.. */ 
    mStacks.get(mCurrentTab).pop(); 

    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction ft = manager.beginTransaction(); 
    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); 
    ft.replace(R.id.realtabcontent, fragment); 
    ft.commit(); 
} 

首頁片段

public class Firstfragment extends BaseFragment 
{ 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

private void setupActionBar() { 
    // TODO Auto-generated method stub 

    actionBar = getSherlockActivity().getSupportActionBar(); 

    actionBar.setDisplayHomeAsUpEnabled(false); 


    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false); 
    View cView = getSherlockActivity().getLayoutInflater().inflate(R.layout.actionbarbutton, null); 
    final TextView cTitletext = (TextView) cView.findViewById(R.id.action_title_home); 
    final ImageButton cSection_Menu = (ImageButton) cView.findViewById(R.id.imgbtn_1); 

    actionBar.setCustomView(cView); 
    cSection_Menu.setImageResource(R.drawable.section); 
    cSection_Menu.setVisibility(View.VISIBLE); 

    cTitletext.setVisibility(View.VISIBLE); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.home_newspaper_list, container,false); 
    lv = (ExpandableListView) view.findViewById(R.id.newspapers_expandable); 

    setHasOptionsMenu(true); 
    return view; 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    flag = true; 
    outState.putBoolean("flag", flag); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    super.onCreateOptionsMenu(menu, inflater); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    flag = true; 
    AppConstants.isFromHomeDetailView = false; 
    index = lv.getFirstVisiblePosition(); 
} 

@Override 
public void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    lv.setSelectionFromTop(index, 0); 

    System.out.println(" onResume Started > "+flag); 
    setupActionBar(); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    HashMap<String, ArrayList<HashMap<String,String>>> nlist = ma.newspaper_list; 
    ArrayList<String> languagelist = ma.languagelist; 

    expListAdapter = new ExpandableListAdapter(getActivity(), languagelist, nlist); 

    lv.setAdapter(expListAdapter); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    return false; 
} 

public class ExpandableListAdapter extends BaseExpandableListAdapter 
{ 

    /* methods for BaseExpandableListAdapterlike getchildcount() etc. */ 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     View view = null; 
     view.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(final View v) { 
       secondfragment nl = new secondfragment(); 
       mActivity.pushFragments(AppConstants.TAB_A,nl,true, true);//****error on that line after resuming activity**.** 
      } 
     }); 
     return view; 
    } 
} 

} 
+0

您的mActivity指針是否得到正確更新? – Niko

+0

'mActivity'get正常更新。但是'commit()'推送片段的問題。 –

+0

你應該檢查你的mA活動是否存在。你能提供你的毫安宣言嗎? – xxxzhi

回答

0

看起來你犯下Fragment當你FragmentActivity是整理並引起厚望。

我會建議創建機制來存儲Fragment所需的信息,並且在您的FragmentActivity處於活動狀態且正在運行且未完成時,請執行Fragment的提交。

0

除了寫作ft.commitAllowingStateLoss();你只需要編寫commit();作爲ft.commitAllowingStateLoss();定義

It is similar to commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

所以如果你摧毀了你的活動之前,那麼它將總是拋出Activity has been destroyed錯誤。

相關問題