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); } } } 用的是按钮的点击事件new出一个匿名内部类,之所以这么做,是不想让主MainAcitity变得臃肿,可是 结果出错了,Log打印出来是java.lang.IllegalStateException: Activity has been destroyed 求大神解答为什么啊?难道不可以这么写吗? 我也是一个新手,才开始写我的第一个APP。求大神解答
alionSSS
相关分类