Attach a Fragment to the Activity

前端 未结 3 1487
野的像风
野的像风 2021-01-07 15:23

I am trying to attach a Fragment to my MainActivity programmatically by using the following code:

@Override
protected void onCreate(Bundle savedInstanceSta         


        
3条回答
  •  礼貌的吻别
    2021-01-07 16:22

    Replace your code:

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MainFragment fragment = new MainFragment();
    fragmentTransaction.add(R.id.activity_main,fragment);
    fragmentTransaction.commit();
    

    By this:

    Fragment fragment = new MainFragment();
    getSupportFragmentManager.begintransaction.replace(R.id.container,fragment).commit;
    

提交回复
热议问题