FragmentTabhost没有活动illegalstateexception错误

时间:2013-09-05 11:43:27

标签: android android-fragments

我正在使用fragmenttabhost进行分片。 在一个片段中有两个子片段。 当改变片段从子片段工作正常。 但是当转到其他选项卡并返回时发生错误

public class MainActivity extends FragmentActivity implements
    OnTabChangeListener {

private FragmentTabHost mTabHost;
private HashMap<String, Stack<Fragment>> mStacks;
private String mCurrentTab;

@Override
protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.bottom_tabs);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    mTabHost.setOnTabChangedListener(this);
    mStacks = new HashMap<String, Stack<Fragment>>();
    mStacks.put(AppConstants.DEAL_FRAGMENT, new Stack<Fragment>());
    mStacks.put(AppConstants.CONN_FRAGMENT, new Stack<Fragment>());
    mStacks.put(AppConstants.PROFILE_FRAGMENT, new Stack<Fragment>());
    mStacks.put(AppConstants.MORE_FRAGMENT, new Stack<Fragment>());

    initializeTabs();
}

private void initializeTabs() {
    Bundle b = new Bundle();
    b.putString("key", AppConstants.DEAL_FRAGMENT);
    mTabHost.addTab(mTabHost.newTabSpec(AppConstants.DEAL_FRAGMENT)
            .setIndicator("Deal"),
            DealFragment.class, b);
    //
    b = new Bundle();
    b.putString("key", AppConstants.CONN_FRAGMENT);
    mTabHost.addTab(mTabHost.newTabSpec(AppConstants.CONN_FRAGMENT)
            .setIndicator("Conn"),
            ConnFragment.class, b);
    b = new Bundle();
    b.putString("key", AppConstants.PROFILE_FRAGMENT);
    mTabHost.addTab(mTabHost.newTabSpec(AppConstants.PROFILE_FRAGMENT)
            .setIndicator("Profile"),
            ProfileFragment.class, b);

    b = new Bundle();
    b.putString("key", AppConstants.MORE_FRAGMENT);
    mTabHost.addTab(mTabHost.newTabSpec(AppConstants.MORE_FRAGMENT)
            .setIndicator("More"),
            MoreFragment.class, b);
}


public void pushFragments(String tag, Fragment fragment,
        boolean shouldAnimate, boolean shouldAdd) {
    /*if (shouldAdd)
        mStacks.get(tag).push(fragment);*/
    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();
}

@Override
public void onTabChanged(String tabId) {
    mCurrentTab = tabId;
    Log.d("Tabid", "" + mCurrentTab);
    if (mStacks.get(tabId).size() == 0) {
        if (tabId.equals(AppConstants.DEAL_FRAGMENT)) {
            pushFragments(tabId, new DealFragment(), false, true);
        } else if (tabId.equals(AppConstants.CONN_FRAGMENT)) {
            pushFragments(tabId, new ConnFragment(), false, true);
        } else if (tabId.equals(AppConstants.PROFILE_FRAGMENT)) {
            pushFragments(tabId, new ProfileFragment(), false, true);
        } else if (tabId.equals(AppConstants.MORE_FRAGMENT)) {
            pushFragments(tabId, new MoreFragment(), false, true);
        }
    } else {
        pushFragments(tabId, mStacks.get(tabId).lastElement(), false, false);
    }
}

public void popFragments() {
    Fragment fragment = mStacks.get(mCurrentTab).elementAt(
            mStacks.get(mCurrentTab).size() - 2);
    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();
}

@Override
public void onBackPressed() {
    if(((BaseFragment)mStacks.get(mCurrentTab).lastElement()).onBackPressed() == false){
        if(mStacks.get(mCurrentTab).size() == 1){
            super.onBackPressed();  // or call finish..
        }else{
            popFragments();
        }
    }else{
    }
}

}

公共类DealFragment扩展BaseFragment实现OnTabChangeListener {

private FragmentTabHost mTabHost;
private String mCurrentTab;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(),
            R.id.menu_settings);
    mTabHost.setOnTabChangedListener(this);
    initializeChildTabHost();
    return mTabHost;
}

private void initializeChildTabHost() {
    Bundle b = new Bundle();
    b.putString("key", AppConstants.LIST_DEAL_FRAGMENT);
    mTabHost.addTab(
            mTabHost.newTabSpec(AppConstants.LIST_DEAL_FRAGMENT)
                    .setIndicator(
                            "List"),
            CategoryListFragment.class, b);
    //
    b = new Bundle();
    b.putString("key", AppConstants.CATEGORY_DEAL_FRAGMENT);
    mTabHost.addTab(
            mTabHost.newTabSpec(AppConstants.CATEGORY_DEAL_FRAGMENT).setIndicator(
                    "Category"),
            CategoryFragment.class, b);
}



@Override
public void onTabChanged(String tabId) {
    mCurrentTab = tabId;
    Log.d("Current  Child Tab", ""+mCurrentTab);
}

}

public class CategoryListFragment extends BaseFragment实现OnClickListener {

Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout, container, false);
    button = (Button)view.findViewById(R.id.addNewFrag);
    button.setOnClickListener(this);
    return view;
}
@Override
public void onClick(View v) {

    if(v==button){
        mActivity.pushFragments(AppConstants.LIST_DEAL_FRAGMENT, new InviteFrndFragment(), true, true);
    }
}

}

public class BaseFragment extends Fragment {     公共MainActivity mActivity;

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

    mActivity = (MainActivity) this.getActivity();
}

public boolean onBackPressed() {
    return false;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

}

}

1 个答案:

答案 0 :(得分:0)

在片段活动中,您必须将先前加载的片段的引用存储在变量中。例如,

声明一个gloabal变量如下:

Fragment lastFragment = null;


public void pushFragments(String tag, Fragment fragment,
        boolean shouldAnimate, boolean shouldAdd) {
   // frag
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    if (shouldAnimate)
        ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);

If(lastFragment==null) {
    ft.replace(R.id.realtabcontent, fragment);
    lastFragment = fragment;
}
else {
    ft.replace(R.id.realtabcontent, LastFragment);
}
    ft.commit();
}