Android Studio 3.4。
在gradle.properties中:
android.useAndroidX=true
android.enableJetifier=true
在 app / build.gradle 中:
def AAVersion = '4.6.0'
dependencies {
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'commons-io:commons-io:2.6'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'org.apache.commons:commons-lang3:3.8.1'
implementation 'com.google.android.gms:play-services:4.3.23'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'edu.vt.middleware:vt-password:3.1.2'
implementation "org.androidannotations:androidannotations-api:$AAVersion"
}
片段
import com.google.android.gms.maps.SupportMapFragment;
import androidx.fragment.app.FragmentManager;
public class AgentsFragmentMapTab extends androidx.fragment.app.Fragment {
private SupportMapFragment mapFrag;
@Override
public void onDestroyView() {
super.onDestroyView();
try {
if (mapFrag != null) {
FragmentManager fragmentManager = thisFragmentActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().remove(mapFrag).commit();
thisFragmentActivity = null;
}
} catch (IllegalStateException e) {
}
}
}
但是在此行中出现运行时错误:
fragmentManager.beginTransaction().remove(mapFrag).commit();
错误:
( 6664): onDestroyView
( 6664): RootFragment#onDestroyView()
(CATCHED)( 6664): Cannot remove Fragment attached to a different FragmentManager. Fragment SupportMapFragment{270ec77 (5411fd0b-1b45-40d3-b306-1452294a4734) id=0x7f0901d1} is already attached to a FragmentManager.
(CATCHED)( 6664): java.lang.IllegalStateException: Cannot remove Fragment attached to a different FragmentManager. Fragment SupportMapFragment{270ec77 (5411fd0b-1b45-40d3-b306-1452294a4734) id=0x7f0901d1} is already attached to a FragmentManager.
(CATCHED)( 6664): at androidx.fragment.app.BackStackRecord.remove(BackStackRecord.java:188)
(CATCHED)( 6664): at com.myproject.AgentsFragmentMapTab.onDestroyView(AgentsFragmentMapTab.java:356)
(CATCHED)( 6664): at androidx.fragment.app.Fragment.performDestroyView(Fragment.java:2852)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:947)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1228)
(CATCHED)( 6664): at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2066)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1856)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1811)
(CATCHED)( 6664): at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManagerImpl.java:1686)
(CATCHED)( 6664): at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:299)
(CATCHED)( 6664): at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:230)
(CATCHED)( 6664): at androidx.viewpager.widget.ViewPager.setAdapter(ViewPager.java:513)
(CATCHED)( 6664): at md.qsystems.android.agents.SelectAgentMainFragment.removeViewPager(SelectAgentMainFragment.java:460)
(CATCHED)( 6664): at md.qsystems.android.agents.SelectAgentMainFragment.reloadAgents(SelectAgentMainFragment.java:553)
(CATCHED)( 6664): at md.qsystems.android.agents.SelectAgentMainFragment.access$000(SelectAgentMainFragment.java:54)
(CATCHED)( 6664): at md.qsystems.android.agents.SelectAgentMainFragment$1.onClick(SelectAgentMainFragment.java:114)
(CATCHED)( 6664): at android.view.View.performClick(View.java:5204)
(CATCHED)( 6664): at android.view.View$PerformClick.run(View.java:21153)
(CATCHED)( 6664): at android.os.Handler.handleCallback(Handler.java:739)
(CATCHED)( 6664): at android.os.Handler.dispatchMessage(Handler.java:95)
(CATCHED)( 6664): at android.os.Looper.loop(Looper.java:148)
(CATCHED)( 6664): at android.app.ActivityThread.main(ActivityThread.java:5417)
(CATCHED)( 6664): at java.lang.reflect.Method.invoke(Native Method)
(CATCHED)( 6664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
(CATCHED)( 6664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
gment_)( 6664): reloadAgents_existing_fragments: []
答案 0 :(得分:0)
迁移到AndroidX后,我遇到了同样的问题。在我的案例中,错误与片段的检索方式有关,然后使用两个不同的管理器进行操作。要获取片段,我使用了didChangeDependencies()
:
initState()
在隐藏它的同时,我使用了getChildFragmentManager
:
this.mSaving = (SavingData)this.getChildFragmentManager().findFragmentById(R.id.fragment_set_edit_saving_progress);
在getFragmentManager
中使用private void hideProgress(){
getFragmentManager()
.beginTransaction()
.hide(this.mSaving)
.commit();
}
解决了我的问题。
希望这会有所帮助