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)