gpt4 book ai didi

java - ChildFragmentManager java.lang.IllegalStateException : Activity has been destroyed

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:04 29 4
gpt4 key购买 nike

我尝试使用 getChildFragmentManager() 在 vi​​ewpager 的另一个 fragment 内添加一个 fragment 。我收到以下错误,

java.lang.IllegalStateException: Activity has been destroyed at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1549) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654) at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:625) at com.lakeba.gameon.userprofile.UserProfileContainerFragment.replaceFragment(UserProfileContainerFragment.java:72)

我试过 this workaround但仍然出现同样的错误。

UserProfileContainerFragment.java

public class UserProfileContainerFragment extends CustomFragment {


private View rootView;
private Fragment fragment1;

public UserProfileContainerFragment() {
// Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

public static UserProfileContainerFragment newInstance() {
UserProfileContainerFragment fragment = new UserProfileContainerFragment();
return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_user_profile_container, container, false);

if(savedInstanceState == null) {
UserProfileFragment userProfileFragment = UserProfileFragment.newInstance();
getChildFragmentManager().beginTransaction()
.replace(R.id.user_profile_container, userProfileFragment)
.commitAllowingStateLoss();
}

return rootView;
}

/*@Override
public void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}*/

public void replaceFragment(Fragment fragment, boolean addToBackStack){
fragment1 = fragment;
if(addToBackStack){
/*getChildFragmentManager().beginTransaction()
.replace(R.id.user_profile_container, fragment)
.addToBackStack(null)
.commit();*/
getChildFragmentManager().beginTransaction()
.replace(R.id.user_profile_container, fragment)
.addToBackStack(null)
.commit();
//.commitAllowingStateLoss();
}
else{
getChildFragmentManager().beginTransaction()
.replace(R.id.user_profile_container, fragment)
.commit();
//.commitAllowingStateLoss();
}

}

@Override
public void onDetach() {
super.onDetach();

try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);

} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

}
}

HomeMapActivity.java

public class HomeMapActivity extends AppCompatActivity implements UserProfileFragment.OnUserProfileFragmentListener{

private Toolbar homeToolbar;
private ViewPager homeViewPager;
private TabLayout homeTabLayout;
private UserProfileContainerFragment userProfileContainerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_map);

/*homeToolbar = (Toolbar) findViewById(R.id.home_toolbar);
setSupportActionBar(homeToolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

homeViewPager = (ViewPager) findViewById(R.id.home_view_pager);

HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getSupportFragmentManager());
userProfileContainerFragment = new UserProfileContainerFragment();
homeViewPagerAdapter.addFragment(new UserProfileContainerFragment(),"Profile");
homeViewPager.setAdapter(homeViewPagerAdapter);

homeTabLayout = (TabLayout) findViewById(R.id.home_tabs);
homeTabLayout.setupWithViewPager(homeViewPager);
setTabIcons(homeTabLayout);


homeTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
tab.setIcon(tabIconsArrayActivated[tabPosition]);
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
tab.setIcon(tabIconsArray[tabPosition]);
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
tab.setIcon(tabIconsArrayActivated[tabPosition]);
}
});

homeTabLayout.getTabAt(1).select();
}

private void setTabIcons(TabLayout homeTabLayout) {
homeTabLayout.getTabAt(0).setIcon(tabIconsArray[0]);
homeTabLayout.getTabAt(1).setIcon(tabIconsArray[1]);
homeTabLayout.getTabAt(2).setIcon(tabIconsArray[2]);
homeTabLayout.getTabAt(3).setIcon(tabIconsArray[3]);
}

@Override
public void onUserProfileEditButtonClicked() {
userProfileContainerFragment.replaceFragment(EditUserProfileFragment.newInstance(),true);
}

private class HomeViewPagerAdapter extends FragmentStatePagerAdapter{

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public HomeViewPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
//return mFragmentTitleList.get(position);
return null;
}

public void addFragment(Fragment fragment,String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
}
}

最佳答案

如果您查看 onCreate() 方法中的代码,您会看到以下几行:

userProfileContainerFragment = new UserProfileContainerFragment();
homeViewPagerAdapter.addFragment(new UserProfileContainerFragment(),"Profile");

您首先使用 UserProfileContainerFragment 的新实例初始化 userprofileContainerFragment 字段,然后在下方创建 UserProfileContainerFragment 的实例以在 ViewPager 中使用(您可以通过放置日志语句轻松观察到这一点在 UserProfileContainerFragment 的构造函数中...您将看到正在创建两个实例)。稍后在您的代码中您尝试使用字段 userProfileContainerFragment 这将导致失败,因为 UserProfileContainerFragment 的实例根本没有附加到 Activity (这不是 ViewPager 使用的 fragment )。

您的代码应如下所示,以维护正确的引用而不创建分离的 fragment :

userProfileContainerFragment = new UserProfileContainerFragment();
homeViewPagerAdapter.addFragment(userProfileContainerFragment,"Profile");

关于java - ChildFragmentManager java.lang.IllegalStateException : Activity has been destroyed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38628281/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com