Home My App is crashing when trying to replace fragments
Reply: 1

My App is crashing when trying to replace fragments

Ash Cook
Ash Cook Published in 2017-11-14 21:47:44Z

I'm working with the preset navigation bar in the android studio activity creation, when I try and replace two fragments the app crashes. I have tried to use both the

import android.app.FragmentManager;

as well as the

import android.support.v4.app.FragmentManager;

I was having trouble defining the fragment to be swapped out with the seconds import so I was not able to test it using that import.

MainActivity Java Script

package acook.mobileassignment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                setTitle("Music Main");
                FragmentManager fragManager = getFragmentManager();
                MusicMain fragment = new MusicMain();
                FragmentTransaction fragTransaction = fragManager.beginTransaction();
                fragTransaction.replace(R.id.mainContent, fragment);

                Context context = getApplicationContext();
                CharSequence text = "MusicPageCheck!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                return true;
            case R.id.navigation_dashboard:
                setTitle("Timer Set");
                FragmentManager fragManager2 = getFragmentManager();
                FragmentTransaction fragTransaction2 = fragManager2.beginTransaction();
                fragTransaction2.replace(R.id.mainContent, new TimerSet()).commit();
                return true;
            case R.id.navigation_notifications:
                FragmentManager fragManager3 = getFragmentManager();
                FragmentTransaction fragTransaction3 = fragManager3.beginTransaction();
                fragTransaction3.replace(R.id.mainContent, new Favourites()).commit();
                return true;
        return false;


protected void onCreate(Bundle savedInstanceState) {

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

heisen Reply to 2017-11-14 21:55:56Z

have you tried using getSupportFragmentManager() with the android.support.v4.app.FragmentManager, instead the getFragmentManager() ?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342751 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO