-1

In my application I want call fragment methods from other fragments, I write below codes but when call this method show me below error in logcat and Force close application.

For show this two fragments into activity I use TabLayout and ViewPager.

My Review Fragment codes:

public void getComments(final Context context) {

    JsonObject requestBean = new JsonObject();
    requestBean.addProperty("entityType", 1);
    requestBean.addProperty("reviewType", 5);
    requestBean.addProperty("reviewUserType", 2);
    requestBean.addProperty("entityID", serialID);
    requestBean.addProperty("celebrityId", 0);
    requestBean.addProperty("pageIndex", 1);
    requestBean.addProperty("pageSize", 10);

    InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
    Call<CommentResponse> call = api.getComments(token, requestBean);

    call.enqueue(new Callback<CommentResponse>() {
        @Override
        public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
            if (response.body().getData() != null) {
                if (response.body().getData().size() > 0) {
                    reviewMovieFrag_NoComment.setText("");
                } else {
                    reviewMovieFrag_NoComment.setText(context.getResources().getString(R.string.noReviews));
                    SerialReview_CastProgress.setVisibility(View.GONE);
                }
                commentModel.clear();
                commentModel.addAll(response.body().getData());
                commentsListAdapter.notifyDataSetChanged();
                reviewMovieFrag_newsCommentsRecyclerView.setAdapter(commentsListAdapter);

                reviewMovieFrag_newsCommentsUserTypeText.setText(userTypeStr);
                reviewMovieFrag_newsCommentsReviewTypeText.setText(reviewTypeStr);

                reviewMovieFrag_Progress.setVisibility(View.GONE);
            }
        }

        @Override
        public void onFailure(Call<CommentResponse> call, Throwable t) {
            reviewMovieFrag_Progress.setVisibility(View.GONE);
        }
    });
}

I want call this method (getComments method) into InfoFragment and for this I write this code :

new MovieDetail_reviewFragment().getComments(getActivity());

But in LogCat show me this error :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.test.app.Fragments.MovieDetailFragments.MovieDetail_reviewFragment$6.onResponse(MovieDetail_reviewFragment.java:301)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Show error for this line :

reviewMovieFrag_NoComment.setText(context.getResources().getString(R.string.noReviews));

ATTENTION : Dear moderators and dear users I know this error for NullPointer but I don't know how can I fix it?
I tried for fix this issue but I can't so I ask in StackOverFlow.
Please help me and don't give me negative points or duplicate my post!

Please help me, Thanks all

Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
Joke
  • 27
  • 1
  • 2

2 Answers2

2

Here,

new MovieDetail_reviewFragment().getComments(getActivity());

Is creating your fragment class newly. You need to use all variables when Fragment class created at starting.

Use viewPager.setOffscreenPageLimit(fragmentNumber); - this will help you to create all fragment when tab initialized. Then use instance to access any fragment method.

Declare at top of fragment class, private static FragmentClass instance = null;

inside your Fragment class override onCreate() and initialize instance,

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
}

Create getInstance() method in fragment contains your calling method,

public static FragmentClass getInstance(){

    return instance;
}

Finally call method from another fragment,

FragmentClass.getInstance().yourMethod();
Exigente05
  • 2,161
  • 3
  • 22
  • 42
0

don't take it otherwise but what you are doing is not good design pattern, you should use interfaces for communicating between any two fragments. It will make things easier for you. Look into this post Basic Communication between two fragments

himel
  • 500
  • 5
  • 14