jeudi 7 juillet 2016

How to fix the error "cannot resolve method 'add(int, com.web"

I am getting an error

cannot resolve method 'add fragmentTransaction.add(R.id.fragment_container,f1);

When I'm trying to run the application it is saying:

unfortunately your application has stopped

Code:

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{
    Button bn;
    boolean status = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn = (Button) findViewById(R.id.bn);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager =getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                if(!status)
                {
                     FragmentOne f1 = new FragmentOne();
                    fragmentTransaction.add(R.id.fragment_container,f1);
                    fragmentTransaction.commit();
                    bn.setText("LOAD SECOND FRAGMENT");
                    status = true;
                }
                else
                {
                  FragmentTwo f2 = new  FragmentTwo();
                    fragmentTransaction.add(R.id.fragment_container,f2);
                    fragmentTransaction.commit();
                    bn.setText("LOAD FIRST FRAGMENT");
                    status = false;

                }

            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire