来自parentFragment的ChildFragmentManager null对象引用

时间:2018-01-02 06:22:48

标签: android android-fragments android-nested-fragment child-fragment

我正在处理一个简单的应用程序并替换父片段顶部的片段。我使用下面的代码打开子片段。这是我在PendingFragment中使用的代码,用于从PendingFragment打开PendingDetailFragment。

Fragment fragment = new PendingDetailFragment();
FragmentManager manager = getParentFragment().getChildFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();

不幸的是,我正在跟踪日志跟踪。

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.Fragment.getChildFragmentManager()' on a null object reference
       at com.naushad.kenostaff.landing.PendingFragment$1.onItemClick(PendingFragment.java:78)
       at com.naushad.kenostaff.util.RecyclerItemClickListener.onInterceptTouchEvent(RecyclerItemClickListener.java:36)
       at android.support.v7.widget.RecyclerView.dispatchOnItemTouch(RecyclerView.java:2261)
       at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2389)
       at android.view.View.dispatchTouchEvent(View.java:9317)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2551)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2241)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2557)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
       at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2440)
       at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1748)
       at android.app.Activity.dispatchTouchEvent(Activity.java:2816)
       at com.naushad.kenostaff.landing.LandingActivity.dispatchTouchEvent(LandingActivity.java:356)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
       at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2401)
       at android.view.View.dispatchPointerEvent(View.java:9537)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4308)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4174)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3702)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3755)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3721)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3865)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3729)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3922)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3702)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3755)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3721)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3729)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3702)
       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6000)
       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5974)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5935)
       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6103)
       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:323)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5437)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

请帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码替换片段。

public static void setReplaceFragment(Fragment fragment, AppCompatActivity appCompatActivity, boolean isAddToBackStack, int containerId, String tagName) {
    FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (isAddToBackStack) {
        fragmentTransaction.replace(containerId, fragment, tagName);
        fragmentTransaction.addToBackStack(null);
    } else {
        fragmentTransaction.replace(containerId, fragment, tagName);
    }
    fragmentTransaction.commit();
}