0

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.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dad
  • 11
  • Please find a very good tutorial on `Fragment`s. It's not easy to help you because there is a lot of things you're doing horribly wrong. – Peter Chaula Jan 08 '17 at 20:39
  • Possible duplicate of the canonical [NPE question](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510)? I have not voted to close as I don't develop for Android, but it looks like a dup. – halfer Jan 08 '17 at 21:16
  • @Idempotence: logcats can just be presented with code formatting. They do not additionally need to be placed in a quote block. – halfer Jan 08 '17 at 21:17
  • I think you are implementing fragment wrongly :/ you should use fragment inside activity and from the activity change them – Yessine Mahdouani Jan 08 '17 at 21:17
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – halfer Jan 09 '17 at 21:04

1 Answers1

1

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();
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Error:(98, 1) error: class, interface, or enum expected – Dad Jan 08 '17 at 21:02
  • 1
    @Dad This is not complete code. You need to do a little bit of work to integrate it properly in your code, although i've given you as much direction as I can. Posting a compiler error for a line number of a class I don't have is not really productive and doesn't give me any more ability to help. – Karakuri Jan 08 '17 at 21:17
  • @Dad: we do expect some effort from your side. The urgent begging and all-caps shouting has already earned you a downvote - don't also expect people to do all your debugging for you. Consider answers here to be helpful clues, with your role being to apply them to your project. – halfer Jan 08 '17 at 21:19