0
public class MainActivity extends FragmentActivity {

    ImageView iview1;
    LayoutParams params;
    FragmentManager fmgr;
    FragmentTransaction ftr;
    FirstFragment ff;
    LinearLayout layout;
    ArrayList<Fragment> FArray;
    int counter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=0;
        FArray=new ArrayList<Fragment>();
        iview1=(ImageView)findViewById(R.id.iview);
        iview1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                showFF();

            }
        });
        layout=(LinearLayout) findViewById(R.id.frag);
        fmgr = getSupportFragmentManager();
        ftr=fmgr.beginTransaction();

        params=(LayoutParams) layout.getLayoutParams();

        //showF();
    }

    public void showFF(){
        FArray.add(new FirstFragment());
        ftr.add(R.id.frag, FArray.get(counter));
        if (true){//showing==false){
            params.weight=1;
            iview1.setLayoutParams(params);
            layout.setLayoutParams(params);
            ftr.commit();
        }
        counter++;

    }
}

The first time this code execute correctly but when I click again on iview1 . I get an Exception -> Force Close. Why?

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98

1 Answers1

0

A problem in your code is that you need to call beginTransaction each time you want to commit your Fragment change.

So try to add the beginTransaction in your function like this:

public void showFF(){
    ftr=fmgr.beginTransaction();
    FArray.add(new FirstFragment());
    ftr.add(R.id.frag, FArray.get(counter));
    if (true){//showing==false){
        params.weight=1;
        iview1.setLayoutParams(params);
        layout.setLayoutParams(params);
        ftr.commit();
    }
    counter++;
}

If you still have your exception, post your Logcat and try to interpret it.

Community
  • 1
  • 1
calimbak
  • 856
  • 13
  • 21