简体   繁体   中英

java.lang.IllegalStateException crash reports from Google Play Console

I have a trouble with my Android app. Some devices which use Android 7.1 or 8.0 often report IllegatStateException crash. I have searched on Google and tried to fix it many times but no use. I cannot figure out what causes this crash. So please help me if you have time. Here is a full report:

java.lang.IllegalStateException: 

  at android.support.v4.app.FragmentManagerImpl.addFragment (FragmentManagerImpl.java:60)

  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:137)

  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManagerImpl.java:38)

  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManagerImpl.java:112)

  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManagerImpl.java:89)

  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManagerImpl.java:21)

  at android.support.v4.app.FragmentManagerImpl$1.run (FragmentManagerImpl.java:2)

  at android.os.Handler.handleCallback (Handler.java:869)

  at android.os.Handler.dispatchMessage (Handler.java:101)

  at android.os.Looper.loop (Looper.java:206)

  at android.app.ActivityThread.main (ActivityThread.java:6733)

  at java.lang.reflect.Method.invoke (Method.java)

  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:845)

I have a main activity contains a FrameLayout. I use this FrameLayout to display 5 different fragments by using:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (button) {
    case 1:
        ft.replace(R.id.mainframe, fragment1, FRAGMENT1_TAG);
        break;
    case 2:
        ft.replace(R.id.mainframe, fragment2, FRAGMENT2_TAG);
        break;
    case 4:
        Fragment fragment4 = new Fragment();
        ft.replace(R.id.mainframe, fragment4, FRAGMENT4_TAG);
        break;
}
ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ft.commit();

Some fragments must be saved to restore later (fragment 1 -> 3), some must be refreshed (create new instance) (fragment 4 -> 5). Moreover, on fragment 4 and 5, there are a viewpager and an array of button to move between pages for each. The code of viewpagers:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager(), new Fragment[] {
            new Page1(),
            new Page2()
    }));
}

private void onButtonTap(Button btn) {
    switch (btn.getId()) {
        case R.id.button1:
            viewPager.setCurrentItem(0, true);
            break;
        case R.id.button2:
            viewPager.setCurrentItem(1, true);
            break;
    }
}

I also define 2 classes derive from Dialog and use Toast. I cannot reproduce on simulators or my devices. It just happens on a few devices. Is there anything wrong with my code? Please help me. This crash stays for a long time, I need to get rid of it. I'm looking forward to hearing from you. Thank you very much.

Long


Edit 1: Here is where I call ft.replace on fragment 1

@Override
protected void onResume() {
    // Check server connection to go back to the first fragment
    if (!ConnectivityReceiver.getInstance().isServerConnected()) {
        Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_frame);
        if (currentFragment != null && 
            (FRAGMENT_4_TAG.equals(currentFragment.getTag()) || FRAGMENT_5_TAG.equals(currentFragment.getTag())) && isActive()) {
            .....
            ft.replace(fragment1);
        }
    }
}

According to the source code, the addFragment method throws that exception with the message "Fragment already added" if you attempt to add a Fragment that is already a member of the manager.

Now, the version of the FragmentManagerImpl source code that I can see looks rather different to the stack trace, so this is a bit of a guess. But I suspect that the problem is that you may be calling ft.replace on fragment2 or fragment1 when they are already part of the layout. Under certain circumstances ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM