为了账号安全,请及时绑定邮箱和手机立即绑定

求大神解答,利用接口实现Fragment切换,为什么Activity被销毁了

MainActivity:
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.bt_user:
            cf.setinitif(new Interface_ChangeFragment() {
                @Override
                public Fragment initif() {
                    return tab_user;
                }

                @Override
                public int initTAG() {
                    return 3;
                }
            });
            break;
        case R.id.bt_find:
            cf.setinitif(new Interface_ChangeFragment() {
                @Override
                public Fragment initif() {
                    return tab_found;
                }

                @Override
                public int initTAG() {
                    return 2;
                }
            });
            break;
        case R.id.bt_watchmoive:
            cf.setinitif(new Interface_ChangeFragment() {
                @Override
                public Fragment initif() {
                    return tab_watchMoive;
                }

                @Override
                public int initTAG() {
                    return 1;
                }
            });
            break;
        case R.id.bt_selectsong:
            cf.setinitif(new Interface_ChangeFragment() {
                @Override
                public Fragment initif() {
                    return tab_selectSong;
                }

                @Override
                public int initTAG() {
                    return 0;
                }
            });
            break;
        case R.id.bt_frist:
            break;
    }
    
    
    
    
    
    
    
    
    
    接口的实现了类:
    public class ChangeFragment extends FragmentActivity {
    int TAG;
    Tab_selectSong tab_selectSong;
    Tab_watchMoive tab_watchmoive;
    Tab_Found tab_found;
    Tab_User tab_user;
    public void setinitif(Interface_ChangeFragment interface_changeFragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fts = manager.beginTransaction();
        if (interface_changeFragment.initif() == null) {
            TAG = interface_changeFragment.initTAG();
            switch (TAG) {
                case 0:
                   tab_selectSong = new Tab_selectSong();
                    fts.add(R.id.activity_fragment,tab_selectSong);
                    hideall();
                    fts.show(tab_selectSong);
                    fts.commit();
                    break;
                case 1:
                    tab_watchmoive = new Tab_watchMoive();
                    fts.add(R.id.activity_fragment,tab_watchmoive);
                    hideall();
                    fts.show(tab_watchmoive);
                    fts.commit();
                    break;
                case 2:
                    tab_found = new Tab_Found();
                    fts.add(R.id.activity_fragment,tab_found);
                    hideall();
                    fts.show(tab_found);
                    fts.commit();
                    break;
                case 3:
                   tab_user = new Tab_User();
                    fts.add(R.id.activity_fragment,tab_user);
                    hideall();
                    fts.show(tab_user);
                    fts.commit();
                    break;
            }
        } else {
            TAG = interface_changeFragment.initTAG();
            switch (TAG) {
                case 0:
                    hideall();
                    fts.show(tab_selectSong);
                    fts.commit();
                    break;
                case 1:
                    hideall();
                    fts.show(tab_watchmoive);
                    fts.commit();
                    break;
                case 2:
                    hideall();
                    fts.show(tab_found);
                    fts.commit();
                    break;
                case 3:
                   hideall();
                    fts.show(tab_user);
                    fts.commit();
                    break;
            }

        }
    }

    private void hideall() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fts = manager.beginTransaction();
        if(tab_selectSong!=null)
        {
            fts.hide(tab_selectSong);
        }
        if(tab_watchmoive!=null)
        {
            fts.hide(tab_watchmoive);
        }
        if(tab_found!=null)
        {
            fts.hide(tab_found);
        }
        if(tab_user!=null)
        {
            fts.hide(tab_user);
        }
    }
}


结果出错了,Log打印出来是java.lang.IllegalStateException: Activity has been destroyed

求大神解答为什么啊?难道不可以这么写吗?


正在回答

1 回答

你是主攻java的么,写得这么费劲。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
多种多样的App主界面Tab实现方法
  • 参与学习       48925    人
  • 解答问题       179    个

本课程就将讲解迄今为止最流行的4种实现Tab的方法

进入课程

求大神解答,利用接口实现Fragment切换,为什么Activity被销毁了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信