Ahmed Elsayed
Ahmed Elsayed

Reputation: 240

I get NullPointerException while trying calling a function from Fragment in another Fragment

I'm trying to call a function from a Fragment in other Fragment to use it . I'm trying to get the AllNewBookingFragment tag and id but I still get this exception

2020-10-15 16:45:10.250 27619-27619/com.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app, PID: 27619
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at com.app.UI.Fragments.OnlineBookingModule.NewBookingFragment.attachResultOfDocAvaDatesToView(NewBookingFragment.java:489)
        at com.app.Presenters.BookingPresenter.onFinishedDocAvaDays(BookingPresenter.java:86)
        at com.app.Repositories.BookingRepository$4.onResponse(BookingRepository.java:79)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

Here is my Fragment that contains the function that I want to call.. I want to call getX()

public class AllNewBookingFragment extends Fragment {
    public AllNewBookingFragment() {
        // Required empty public constructor
    }

    FrameLayout frameLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_all_new_booking, container, false);
        NewBookingFragment newBookingFragment = new NewBookingFragment();
        changeViewBooking(newBookingFragment, TagsConsts.NEW_BOOKING);
        return view;
    }

    public void changeViewBooking(Fragment fragment, String tag){
        FragmentManager fragmentManager = getFragmentManager();
        assert getFragmentManager() != null;
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.bookingFrame,fragment,tag);
        transaction.addToBackStack(null);
        transaction.commit();
    }
    public String getx(){
        return "some text";
    }
}

Here is its xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UI.Fragments.OnlineBookingModule.AllNewBookingFragment"
    android:id="@+id/bigView"
   >


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bookingFrame"
        android:tag="@string/all_tag"/>
</LinearLayout>

Here is the code which is written for calling getx() function in the other Fragment which is called NewBookingFragment I call this method in a callback function in the Fragment

String x = ((AllNewBookingFragment) Objects.requireNonNull(Objects.requireNonNull(getActivity())
                        .getSupportFragmentManager()
                        .findFragmentByTag(getString(R.string.all_tag)))).getx();

Upvotes: 1

Views: 51

Answers (2)

Sinh Phan
Sinh Phan

Reputation: 1266

You are used two different Fragmentmanager.

Use the same FragmentManager:

getFragmentManager().beginTransaction().replace(R.id.bookingFrame, fragment, tag).addToBackStack(null).commit()

And

getFragmentManager().findFragmentByTag(tag)

Upvotes: 1

Javad Dehban
Javad Dehban

Reputation: 1292

can you use static

fragment

public static String getx()
{
  return "some text"; 
} 

Static methods are the methods in Java that can be called without creating an object of the class.

solution 2 : you can new fragment where so call function

 FragmentManager fm = getFragmentManager(); 
 AllNewBookingFragment fragment= (AllNewBookingFragment)fm.findFragmentById(R.id.fragment); 
 fragment.getx(); 

but should not fragment closed

Upvotes: 1

Related Questions