Android Fragment results in NPE "Attempt to invoke virtual method" error

600 Views Asked by At

I want to add button to a fragment to call another fragment. I added by the following code.

package com.munz.lop;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    public HomeFragment() {


        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction ();
        HomeFragment homeFragment = new HomeFragment ();


        transaction.add (R.id.relativelayout_for_fragment, homeFragment  );
        transaction.commit ();
        // Required empty public constructor
    }

    public void onSelectFragment(View view){

        Fragment newFragment;

        if(view == view.findViewById (R.id.btnAbout)){
            newFragment = new AboutFragment ();

        } else if(view == view.findViewById (R.id.btnFeedback)){
            newFragment = new FeedbackFragment ();


        } else if (view == view.findViewById (R.id.btnContacts)){
            newFragment = new ContactUsFragment ();



    } else {
            newFragment = new HomeFragment ();

        }

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace ( R.id.relativelayout_for_fragment, newFragment );
        transaction.addToBackStack ( null );
        transaction.commit ();
    }





}

It have no error but it I open this menu in Slider Menu it Force Stops the application with the logcat below:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.munz.lop, PID: 27402
                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
                          at com.munz.lop.HomeFragment.<init>(HomeFragment.java:32)
                          at com.munz.lop.SliderActivity.onNavigationItemSelected(SliderActivity.java:100)
                          at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
                          at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                          at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                          at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308)
                          at android.view.View.performClick(View.java:5716)
                          at android.view.View$PerformClick.run(View.java:22596)
                          at android.os.Handler.handleCallback(Handler.java:739)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:148)
                          at android.app.ActivityThread.main(ActivityThread.java:7325)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Application terminated.
1

There are 1 best solutions below

3
On

Your fragment's no-argument constructor should be empty; get rid of everything that's there. You should be creating the fragment and using the FragmentTransaction in the place you handle the menu item click, not in the constructor of the fragment.

void onNavigationItemSelected() {
    HomeFragment fragment = new HomeFragment();
    getFragmentManager().beginTransaction()
            .add(R.id.relativelayout_for_fragment, fragment)
            .commit();
}