java – Android Fragment null对象mNextAnim内部Crach

我正在处理的应用程序的用户遇到了崩溃问题.这是我从控制台获得的堆栈跟踪.

java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:924)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1605)
at android.support.v4.app.FragmentManagerImpl$4.run(FragmentManager.java:532)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

崩溃只发生在拥有5.x的Galaxy Note3或Galaxy S4手机的用户,或者至少是我们唯一报告但却已经发生的手机.它似乎发生在片段之间的过渡或有时从相机返回时.这是我的过渡方法.

public void popBackStackToOr(String popBackFragmentName, String secondPopBackFragment)
{
    int backStackId = 0;
    int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

    List<Fragment> fragments = getSupportFragmentManager().getFragments();

    for (int k = (backStackCount - 1); k > 0; k--)
    {
        String aFragmentClass = fragments.get(k).getClass().toString();
        String[] splitList = aFragmentClass.split("\\.");
        aFragmentClass = splitList[splitList.length - 1];

        boolean firstOptionTrue  = aFragmentClass.equalsIgnoreCase(popBackFragmentName);
        boolean secondOptionTrue = aFragmentClass.equalsIgnoreCase(secondPopBackFragment);

        if ( (firstOptionTrue) || (secondOptionTrue) )
        {
            backStackId = getSupportFragmentManager().getBackStackEntryAt(k).getId();
            break;
        }
    }

    getSupportFragmentManager().popBackStack(backStackId, android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE); //Immediate
}

public void popBackStackForReview(String popBackFragmentName)
{
    int backStackId = 0;
    int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

    List<Fragment> fragments = getSupportFragmentManager().getFragments();

    for (int k = (backStackCount - 1); k > 0; k--)
    {
        String aFragmentClass = fragments.get(k).getClass().toString();
        String[] splitList = aFragmentClass.split("\\.");
        aFragmentClass = splitList[splitList.length - 1];
        if (aFragmentClass.equalsIgnoreCase(popBackFragmentName))
        {
            backStackId = getSupportFragmentManager().getBackStackEntryAt(k).getId();
            break;
        }
    }

    getSupportFragmentManager().popBackStack(backStackId, android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

public void transitionToLoginPage()
{
    Fragment newFragment = new LoginPage();

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.frame_container, newFragment);
    //transaction.addToBackStack(null);

    transaction.commitAllowingStateLoss();
}

public void transitionToNewScreen(Fragment newFragment, Bundle bundle)
{
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    if (currentFragment != null)
    {
        transaction.remove(currentFragment);
    }

    currentFragment = newFragment;

    if (bundle != null)
    {
        newFragment.setArguments(bundle);
    }

    transaction.replace(R.id.frame_container, newFragment);
    transaction.addToBackStack("dummy");

    transaction.commitAllowingStateLoss();
}

public void transitionToNewScreenAndEmptyStack(Fragment newFragment)
{
    getSupportFragmentManager().popBackStack(0, android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);

    currentFragment = newFragment;

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, newFragment);
    transaction.addToBackStack(null);
    transaction.commitAllowingStateLoss();
}

这些是在应用程序中进行转换的唯一方法.所以我假设它来自其中一个.但堆栈跟踪不会回到我写的任何代码,所以我不确定.我发现类似的问题,这是一个Android 5.x问题,没有修复.所以任何帮助删除这个问题都会很棒

这是因为你使用null值调用remove()add()等方法.

 请记住,逐个替换()= remove() – > add().

翻译自:https://stackoverflow.com/questions/31391356/android-fragment-null-object-mnextanim-internal-crach

转载注明原文:java – Android Fragment null对象mNextAnim内部Crach