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
------可能我说的你之前都懂,个人能力低微,已经尽力啦,能有帮助就最好-----
- 1 回答
- 0 关注
- 1201 浏览
添加回答
举报
0/150
提交
取消