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

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

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

Rick_James 2016-12-20 00:30:20
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。求大神解答
查看完整描述

1 回答

已采纳
?
alionSSS

TA贡献57条经验 获得超82个赞

能说下什么时候报的错吗?

--->个人推测可能是ChangeFragment extends FragmentActivity与MainActivity冲突的问题。(我多嘴一下,Fragment类和FragmentActivity类并不是继承关系)

【另外】你说你是新手,那我推荐一个开发中比较不错的用法吧(也是谷歌官方推荐的):使用ViewPager+Fragment的形式,实现fragment页面之间的切换,具体使用可以参考这里:http://www.imooc.com/learn/198 或者 http://www.imooc.com/learn/273

------可能我说的你之前都懂,个人能力低微,已经尽力啦,能有帮助就最好-----

查看完整回答
反对 回复 2016-12-24
  • Rick_James
    Rick_James
    都忘了还有这么一个问题了,我已经解决了。具体是怎么回事也忘了。好像是是我有个地方马虎了 ,不过还是谢谢你,
  • 1 回答
  • 0 关注
  • 1201 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信