如何在popbackstack之后导航到下一个片段

时间:2015-06-22 13:47:00

标签: android fragment

我有n个片段(包含动态视图)。让我们说片段A-> B-> C.现在当我按下(使用popbackstack)时,它的A-> B。现在当点击下一步转到Fragment C时,我在“getFragmentManager”上得到一个空指针异常。如何实现这一点。

@Override
public void onBackPressed() {
    if(getFragmentManager().getBackStackEntryCount() > 0) {
        RequirementFragment.reqIndex = RequirementFragment.reqIndex -1;
        getFragmentManager().popBackStack();
    }
    else
        super.onBackPressed();
}

应用程序中有一个下一个按钮,它接收下一个片段。下面的代码是下一个按钮onclick funtion。

private void goToNextQuestion() {

        Activity parentActivity = getActivity();
        FragmentManager fm = parentActivity.getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        RequirementFragment nextQuestion = new RequirementFragment();
        nextQuestion.setArguments(getFragmentAttribs(reqIndex + 1));

        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
        ft.replace(R.id.s6_lin_layout_question_wrapper, nextQuestion);
        ft.addToBackStack(String.valueOf(reqIndex));
        ft.commit();
    }

日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference
            at mobileapp.fragments.RequirementFragment.getTextBoxSubmission(RequirementFragment.java:605)
            at mobileapp.fragments.RequirementFragment.saveRequirementSubmission(RequirementFragment.java:774)
            at mobileapp.fragments.RequirementFragment.goToNextQuestion(RequirementFragment.java:836)
            at com.localoye.mobileapp.fragments.RequirementFragment.access$100(RequirementFragment.java:73)
            at com.localoye.mobileapp.fragments.RequirementFragment$2.onClick(RequirementFragment.java:149)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            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:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

2 个答案:

答案 0 :(得分:0)

试试这段代码..

private void loadFragment(Fragment fragment, String classTag ) {
        FragmentManager fragmentManager = getFragmentManager();

        if(fragmentManager.findFragmentByTag(classTag) != null) {
            LogUtility.printInfoMessage(CLASSTAG+":loadFragment -- fragment already loaded "+classTag);
            return;
        }

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.summaryFragmentContainer, fragment, classTag);
        fragmentTransaction.commit();
    }

答案 1 :(得分:0)

解决。 popBackStack()删除当前片段的实例,因此当您再次调用已使用(callfragmentbytag)弹出的片段时,它将返回null。因此,唯一可行的方法是在这种情况下“添加/隐藏”片段,而不是使用“替换”。

覆盖后退按钮以取回之前的片段。