我需要我的片段在它们是活动片段时始终调用某个函数。我正在使用viewPager,所以根据this site
我创建了一个名为FragmentLifecycle.java的接口类:
public interface FragmentLifecycle {
public void onResumeFragment();
}
和我实现了我的片段的接口:
@Override
public void onResumeFragment() {
Log.i(TAG, "onResumeFragment() running");
Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
}
在我的活动中:
vpPager = (ViewPager) findViewById(R.id.vpg_main_content);
adapterFragment = new PagerAdapterFragment(getSupportFragmentManager());
vpPager.setAdapter(adapterFragment);
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int newPosition) {
Toast.makeText(ProfileActivity.this, "Selected page position: " + newPosition, Toast.LENGTH_SHORT).show();
if (newPosition == 0) {
FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapterFragment.getItem(0);
fragmentToHide.onResumeFragment();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
我的片段适配器:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return ProfileFragment.newInstance(0, "Profile");
case 1: // Fragment # 0 - This will show FirstFragment different title
return EditProfileFragment.newInstance(1, "Edit");
default:
return null;
}
}
当我运行我的应用程序时,我得到了Null指针:
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:93)
at android.widget.Toast.makeText(Toast.java:241)
at com.example.sayres.myapplication7.mvp.view.profile.ProfileFragment.onResumeFragment(ProfileFragment.java:117)
at com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity$1.onPageSelected(ProfileActivity.java:56)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1862)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:625)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:609)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2176)
at android.view.View.dispatchTouchEvent(View.java:7706)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我的片段Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
出现错误。你的想法是什么?
答案 0 :(得分:0)
在你的片段中试试这个:
protected Activity mActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
现在在Toast
中使用它:
if (isAdded()) {
Toast.makeText(mActivity, "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您收到该异常,因为当您在fragmentToHide.onResumeFragment()
回调中呼叫onPageSelected()
时,该活动尚未附加到该片段。
而是覆盖setUserVisibleHint(boolean isVisibleToUser)
以确定片段是否对用户可见。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Your onResumeFragment() code here.
if (isVisibleToUser) {
Log.i(TAG, "onResumeFragment() running");
Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
}
}
有关详细信息,请参阅此问题:How to determine when Fragment becomes visible in ViewPager