Fragment的add和replace
下面的代码,当先点击第二个TextView的时候,就会抛出java.lang.IllegalStateException:Fragmentalreadyadded异常,而...
下面的代码,当先点击第二个TextView的时候,就会抛出java.lang.IllegalStateException: Fragment already added异常,而如果先点击第一个或第三个TextView的时候,就正常运行。且如果先点击第一个TextView二后点击第二个TextView的时候,也正常运行。这到底是什么原因?三个TextView的代码明显都一样的啊
fragment1 = new FragmentOne();
fragment2 = new FragmentTwo();
fragment3 = new FragmentThree();
System.out.println("*************************************");
System.out.println(fragment1);
System.out.println(fragment2);
System.out.println(fragment3);
System.out.println("*************************************");
ft = fm.beginTransaction();
ft.add(R.id.fragmentcontent, fragment1);
ft.add(R.id.fragmentcontent, fragment2);
ft.add(R.id.fragmentcontent, fragment3);
ft.commit();
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment1!=null){
ft.replace(R.id.fragmentcontent, fragment1);
}
if(fragment1==null){
fragment1 = new FragmentOne();
ft.replace(R.id.fragmentcontent, fragment1);
}
ft.show(fragment1);
ft.commit();
}
});
textView2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment2!=null){
ft.replace(R.id.fragmentcontent, fragment2);
}
if(fragment2==null){
fragment2 = new FragmentTwo();
ft.replace(R.id.fragmentcontent, fragment2);
}
ft.show(fragment2);
ft.commit();
}
});
textView3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment3!=null){
ft.replace(R.id.fragmentcontent, fragment3);
}
if(fragment3==null){
fragment3 = new FragmentThree();
ft.replace(R.id.fragmentcontent, fragment3);
}
ft.show(fragment3);
ft.commit();
}
});
到底是什么问题啊 展开
fragment1 = new FragmentOne();
fragment2 = new FragmentTwo();
fragment3 = new FragmentThree();
System.out.println("*************************************");
System.out.println(fragment1);
System.out.println(fragment2);
System.out.println(fragment3);
System.out.println("*************************************");
ft = fm.beginTransaction();
ft.add(R.id.fragmentcontent, fragment1);
ft.add(R.id.fragmentcontent, fragment2);
ft.add(R.id.fragmentcontent, fragment3);
ft.commit();
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment1!=null){
ft.replace(R.id.fragmentcontent, fragment1);
}
if(fragment1==null){
fragment1 = new FragmentOne();
ft.replace(R.id.fragmentcontent, fragment1);
}
ft.show(fragment1);
ft.commit();
}
});
textView2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment2!=null){
ft.replace(R.id.fragmentcontent, fragment2);
}
if(fragment2==null){
fragment2 = new FragmentTwo();
ft.replace(R.id.fragmentcontent, fragment2);
}
ft.show(fragment2);
ft.commit();
}
});
textView3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ft = fm.beginTransaction();
hide(ft);
if(fragment3!=null){
ft.replace(R.id.fragmentcontent, fragment3);
}
if(fragment3==null){
fragment3 = new FragmentThree();
ft.replace(R.id.fragmentcontent, fragment3);
}
ft.show(fragment3);
ft.commit();
}
});
到底是什么问题啊 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询