Reputation: 277
I'm working with navbar in android studio. I'm trying to replace fragments through getSupportFragmentManager().beginTransaction().replace() but it is not working and continuously giving me error. Here is the code:
public class UserNavbar extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mydrawerlayout;
private ActionBarDrawerToggle atoggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_navbar);
mydrawerlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
atoggle = new ActionBarDrawerToggle(this,mydrawerlayout,R.string.open,R.string.close); //Done
mydrawerlayout.addDrawerListener(atoggle);
atoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.home:
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new User_Home_Fragment()).commit();
break;
case R.id.personalinformation:
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new Personal_Info_Frag()).commit();
break;
case R.id.settings:
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new Settings_Frag()).commit();
break;
}
mydrawerlayout.closeDrawer(GravityCompat.START);
return true;
}}
The User_Home_Fragment(), Personal_Info_Frag() & Settings_Frag() are developed. I will post them too if needed.
I'll be thankful if someone has any suggestions. Regards
Upvotes: 0
Views: 74
Reputation: 1033
I think you missed the new keyword becase replace method have two arguments first one is container id and second one is fragment instance.
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new User_Home_Fragment()).commit();
Upvotes: 1