为什么Android O失败了“不属于这个FragmentManager!”

我已经将应用程序迁移到Android Studio 3中的Android O 3

在Android O模拟器上运行我的所有对话范围现在失败: -

  1. java.lang.IllegalStateException: Fragment MyDialogFragment{43ccf50 #2 MyDialogFragment} declared target fragment SettingsFragment{ceed549 #0 id=0x7f0f0142 android:switcher:2131689794:0} that does not belong to this FragmentManager!
  2. at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1316)
  3. at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1624)
  4. at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1689)
  5. at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
  6. at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2470)
  7. at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2260)
  8. at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2213)
  9. at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2122)
  10. at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:746)
  11. at android.os.Handler.handleCallback(Handler.java:769)
  12. at android.os.Handler.dispatchMessage(Handler.java:98)
  13. at android.os.Looper.loop(Looper.java:164)
  14. at android.app.ActivityThread.main(ActivityThread.java:6535)
  15. at java.lang.reflect.Method.invoke(Native Method)
  16. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  17. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我没有进行任何代码更改。

现在正在使用的对话fragments现在失败显示的Android O中发生了什么变化?

  1. Android Studio 3.0 Canary 1
  2. Build #AI-171.4010489, built on May 15, 2017
  3. JRE: 1.8.0_112-release-b736 x86_64
  4. JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
  5. Mac OS X 10.11.6
  6. compileSdkVersion 'android-O'
  7. buildToolsVersion "26.0.0-rc2"
  8. AndroidManifest.xml
  9. defaultConfig {
  10. minSdkVersion 16
  11. targetSdkVersion 'O'
  12. }
  13. compile 'com.android.support:appcompat-v7:26.0.0-beta1'
  14. compile 'com.android.support:cardview-v7:26.0.0-beta1'
  15. compile 'com.android.support:design:26.0.0-beta1'
  16. compile 'com.android.support:percent:26.0.0-beta1'
  17. dependencies {
  18. classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
  19. }

看答案

我遇到了同样的问题,绝对是一个Android错误。当您使用它作为目标显示出另一个片段的片段时,就会发生这种情况。作为解决方法,您可以使用:

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  2. getActivity().getFragmentManager().beginTransaction().add(dialogFrag, "dialog").commit();
  3. else
  4. getChildFragmentManager().beginTransaction().add(dialogFrag,"dialog").commit();

未经允许不得转载:迪欧吧_技术交流_资源分享_热点资讯_免费VPS空间 » 为什么Android O失败了“不属于这个FragmentManager!”