2

I have a main Activity with some elements. One of them is a Button. I want to load a Fragment when the button is clicked. I want the Fragment to cover the whole Activity, not just be in a layout in it. Then in the Fragment there is also a Button that has to load another Fragment that covers everything, then another one and so on.

So I set an OnClickListener on the Activity to load the first of the Fragments. The Activity loads just fine. If I click buttons for other Activities everything is good. But no matter what I tried it always crashes when I click the Button that is supposed to load the Fragment.

Here's the main Activity's code:

public class SignUpActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    Button next = (Button) findViewById(R.id.button_next_name);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            SignNameFragment nameFragment = new SignNameFragment();
            fragmentTransaction.add(PROBABLY_WHAT_I_AM_DOING_WRONG,nameFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
}
}

All the xml are pretty much the same. RelativeLayout for the Activity, FrameLayout for the Fragments and ScrollView, ImageView, EditText, TextView and Button for all of them.

Things I tried so far according to suggestions on the web:

  • The main Activity's xml uses a RelativeLayout. I tried to load the Fragment in it, but it failed
  • I changed it to FrameLayout
  • I used fragmentTransaction.add(R.id.fragment_container,nameFragment)
  • Instead of using add(), I tried replace(R.id.activity_sign_up,nameFragment)
  • Extending to FragmentActivity
  • Setting a FrameLayout as a Fragment container in my main Activity's xml

I believe my mistake is that I don't set where I want to add the Fragment in the right way.

I am pretty sure it's gonna be a rookie mistake, but I've never used Fragments before. So any suggestions?

Thanks

EDIT: That's the error when I click the button

E/AndroidRuntime: FATAL EXCEPTION: main Process: be.test.test, PID: 16763 java.lang.RuntimeException: be.test.test.SignUpActivity@276634a3 must implement OnFragmentInteractionListener at layout.SignNameFragment.onAttach(SignNameFragment.java:84) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043) at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838) at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:861) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6145) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Yannis Krz
  • 63
  • 1
  • 1
  • 8

2 Answers2

1

you should use a FrameLayout in the activity_sign_up.xml and the replace FragmentManager method to replace a fragment with another fragment

fragmentManager.beginTransaction().replace(R.layout.activity_sign_up, nameFragment).commit();

I´ll use your code like this:

public class SignUpActivity extends AppCompatActivity {

            private FragmentManager fragmentManager;
            private Fragment switchFragment;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_sign_up);

                fragmentManager = getSupportFragmentManager();

                switchFragment nameFragment = new SignNameFragment();
                fragmentManager.beginTransaction()
                   .add(R.layout.activity_sign_up, switchFragment)
                   .commit();


            }

           public void cambiarFragment(){
                 switchFragment = new anyTypeFragment();
                 fragmentManager.beginTransaction()
                    .replace(R.layout.activity_sign_up, switchFragment)
                    .commit();
              }
    }

and the nameFragment class:

public class nameFragment extends Fragment {

    private Button next;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Inicializar Modelo
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.nameFragment_fragment_layout,container,false);    ///Cargar XML, Vista Padre, Si quiero dejarlo siempre

        next=(Button)v.findViewById(R.id.btn_crear_grupo);

        next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        ((SignUpActivity) getActivity()).cambiarFragment();
                    }


                }
            });



        return v;


    }
}

there you can create any Fragment class you want.

yulian6766
  • 26
  • 6
1

Your error says it all, you are not implementing an interface.

Your SignUpActivity need to implement interface OnFragmentInteractionListener and then you must override its methods.

Should be something like this:

public class SignUpActivity extends AppCompatActivity implements SignNameFragment.OnFragmentInteractionListener {
    //implement the interface methods here
}
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • That was it. I guess all the examples that I checked were older and didn't use OnFragmentInteractionListener. The strange thing is that I didn't actually use it anywhere, but without it the app crashes. – Yannis Krz Oct 15 '16 at 17:11
  • You can mark it as accepted if it had helped you, if you are not using the interface you can remove it anytime you want – Nongthonbam Tonthoi Oct 15 '16 at 17:12
  • I will probably need it later, to transfer data. But I don't get why I have to implement it if it isn't being used. – Yannis Krz Oct 15 '16 at 17:22