Search code examples
javaandroidandroid-fragmentsbottomnavigationview

App crash on selecting double time BottomNavigation Item


I'm trying to create an app using BottomNavigationView. I added three fragments it works only once but when i try to reselect an item from botton view app crashes.Here's my mainActivity.java code

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
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.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private TextView mTextMessage;

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;


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

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

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        //fragmentTransaction.add(R.id.fragment, homeFragment).commit();


    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {


        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragmentTransaction.replace(R.id.fragment, new HomeFragment()).commit();
                return true;
            case R.id.navigation_dashboard:
                  fragmentTransaction.replace(R.id.fragment, new Dashboardfragment()).commit();
                return true;
            case R.id.navigation_notifications:
                    fragmentTransaction.replace(R.id.fragment, new NotificationFragment()).commit();
                return true;
        }
        return false;
    }


}

and one of my fragment .java code as below.before that i was facing a different problem where i cant replace my fragment means my home fragment remains there even if i try to change.

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (container != null) {
            container.removeAllViews();
        }

        View view  = inflater.inflate(R.layout.fragment_home,container,false);

        return view;
    }
}

ok Here's my log File:

10-07 02:38:05.768 22895-22964/com.example.anonymous.bottomnavigationbar I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128_msm8916_64_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128__release_AU (I55c48cad9a)
                                                                                       OpenGL ES Shader Compiler Version: E031.25.03.04
                                                                                       Build Date: 03/21/17 Tue
                                                                                       Local Branch: 
                                                                                       Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128
                                                                                       Local Patches: NONE
                                                                                       Reconstruct Branch: NOTHING
10-07 02:38:05.788 22895-22964/com.example.anonymous.bottomnavigationbar I/OpenGLRenderer: Initialized EGL, version 1.4
10-07 02:38:05.848 22895-22964/com.example.anonymous.bottomnavigationbar D/OpenGLRenderer: Enabling debug mode 0
10-07 02:38:05.928 22895-22895/com.example.anonymous.bottomnavigationbar W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-07 02:38:12.918 22895-22895/com.example.anonymous.bottomnavigationbar D/AndroidRuntime: Shutting down VM
10-07 02:38:12.918 22895-22895/com.example.anonymous.bottomnavigationbar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                           Process: com.example.anonymous.bottomnavigationbar, PID: 22895
                                                                                           java.lang.IllegalStateException: commit already called
                                                                                               at android.app.BackStackRecord.commitInternal(BackStackRecord.java:714)
                                                                                               at android.app.BackStackRecord.commit(BackStackRecord.java:705)
                                                                                               at com.example.anonymous.bottomnavigationbar.MainActivity.onNavigationItemSelected(MainActivity.java:44)
                                                                                               at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                               at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:821)
                                                                                               at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
                                                                                               at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:968)
                                                                                               at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:95)
                                                                                               at android.view.View.performClick(View.java:4781)
                                                                                               at android.view.View$PerformClick.run(View.java:19907)
                                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                               at android.os.Looper.loop(Looper.java:160)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5541)
                                                                                               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:964)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
10-07 02:38:12.968 22895-22895/com.example.anonymous.bottomnavigationbar I/Process: Sending signal. PID: 22895 SIG: 9
10-07 02:38:12.968 22895-22895/com.example.anonymous.bottomnavigationbar V/Process: killProcess [22895] Callers=com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:99 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 <bottom of call stack> 

Solution

  • ok now I solved this error simply by creating a method and creating and initializing FragmentManager and FragmentTrasaction there and it works great for me. May be error was due to the fact that we can commit FragmentTransaction only for once for multiple commit we need to declare it again and again btw thnks. here's my method:

    private void openFragment(final Fragment fragment)   {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
    
        }