[英]how to call method of one fragment from another fragment class in android
[英]How to call one Fragment Method from another Fragment in android
在我的应用程序中,我需要Activity
两个fragments
。 为了显示这两个fragments
我使用ViewPager
。
在Fragment
2中,我有一个方法,我想从Fragment
1中调用此方法!
我在第二部分中的方法:
public void getComments() {
JsonObject requestBean = new JsonObject();
requestBean.addProperty("entityType", 4);
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) {
reviewSerialFrag_NoComment.setText("");
} else {
reviewSerialFrag_NoComment.setText(context.getResources().getString(R.string.noReviews));
}
commentModel.clear();
commentModel.addAll(response.body().getData());
commentsListAdapter.notifyDataSetChanged();
reviewSerialFrag_newsCommentsRecyclerView.setAdapter(commentsListAdapter);
reviewSerialFrag_newsCommentsUserTypeText.setText(userTypeStr);
reviewSerialFrag_newsCommentsReviewTypeText.setText(reviewTypeStr);
reviewSerialFrag_Progress.setVisibility(View.GONE);
}
}
@Override
public void onFailure(Call<CommentResponse> call, Throwable t) {
reviewSerialFrag_Progress.setVisibility(View.GONE);
}
});
}
并使用片段一中的以下代码调用此方法:
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<SendCommentResponse> call = api.getSendComment(token, sendData);
showView(loadProgress);
goneView(sendBtn);
call.enqueue(new Callback<SendCommentResponse>() {
@Override
public void onResponse(Call<SendCommentResponse> call, Response<SendCommentResponse> response) {
if (response.body().getData()) {
Alerter.create(getActivity())
.setText(context.getResources().getString(R.string.successSendComment))
.setDuration(2000)
.setIcon(R.drawable.ic_tick_new)
.setBackgroundColorRes(R.color.colorPrimary)
.enableSwipeToDismiss()
.enableProgress(true)
.setOnShowListener(new OnShowAlertListener() {
@Override
public void onShow() {
watchlistDialog.dismiss();
goneView(loadProgress);
showView(sendBtn);
}
})
.setOnHideListener(new OnHideAlertListener() {
@Override
public void onHide() {
infoEpisodeFrag_addWatchList.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_eye_white));
infoEpisodeFrag_addWatchList.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#1da8b9")));
getData();
new EpisodeDetail_ReviewFrag().getComments();
}
})
.setProgressColorRes(R.color.whiteMe)
.show();
}
}
@Override
public void onFailure(Call<SendCommentResponse> call, Throwable t) {
}
});
但是向我展示LogCat中的此错误:
FATAL EXCEPTION: main
Process: com.example.app, PID: 11978
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
at com.example.app.Fragments.EpisodeDetailFrags.EpisodeDetail_ReviewFrag$6.onResponse(EpisodeDetail_ReviewFrag.java:305)
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)
在Fragment
2中显示此行的错误:
reviewSerialFrag_NoComment.setText(context.getResources().getString(R.string.noReviews));
我该如何解决? 请帮我
无需调用getResources()
方法。 只是getString(R.string.noReviews)
起作用。
获取片段的实例
ExampleFrag frag=(ExampleFrag)getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment2);
然后调用任何方法
frag.myMethod();
像常规类一样使用Fragments确实是一个坏主意。 即使您想传递简单的数据,也可以使用实例和包。
如果您的方法不依赖于片段本身,则创建两个片段共享的单独的实用程序类。 只需将其传递给上下文,以便它可以解析其中的一些变量。
在片段中的单独类中将UI操作分开。 创建该实用程序类的侦听器,并更改片段中的视觉状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.