gpt4 book ai didi

android - 当布局不为空时得到 "java.lang.IllegalStateException: Fragment already active"

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:30 28 4
gpt4 key购买 nike

<分区>

我有 3 个 fragment 的 Activity 和寻呼机。我使用选项卡进行切换。当我单击某个按钮时,我想用新的替换 fragment0。仅当 fragment0 具有空布局时才有效。如果我在 fragment0 的布局中设置了一些内容(必须有 listview),则会抛出异常:

java.lang.IllegalStateException: Fragment already active
at android.support.v4.app.Fragment.setInitialSavedState(Fragment.java:503)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:110)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)

我使用以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/all_exp_frag"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/all_expenses"/>

</LinearLayout>

Activity :

public class Activity extends FragmentActivity {

private List<Fragment> fragments;
private MyPagerAdapter pagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
fragments = asList(new Fragment0(), new Fragment1(), new Fragment2());
// initialize pager, etc.
pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), pager);
}

// handle add new fragment button
@Override
public boolean onOptionsItemSelected(MenuItem item) {
pagerAdapter.replaceCurrentFragment(new Fragment3());
}

public class MyPagerAdapter extends FragmentStatePagerAdapter {

final ViewPager pager;

public MyPagerAdapter(FragmentManager fm, ViewPager pager) {
super(fm);
this.pager = pager;
}

@Override
public CharSequence getPageTitle(int position) {
return fragments.get(position).getFragmentTitle();
}

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

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

@Override
public int getItemPosition(Object object) {
if (object instanceof ExpensesFragment) {
return POSITION_NONE;
}
return POSITION_UNCHANGED;
}

public void replaceCurrentFragment(BaseFragment fragment) {
final int currentPosition = pager.getCurrentItem();
final BaseFragment old_fragment = fragments.get(currentPosition);
if (old_fragment == null) {
return;
}
this.startUpdate(pager);
getSupportFragmentManager()
.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.remove(old_fragment)
.add(pager.getId(), fragment)
.commitAllowingStateLoss();
fragments.set(currentPosition, fragment);
this.finishUpdate(pager);
this.notifyDataSetChanged(); //IN THIS LINE EXCEPTION IS THROWN WHEN LAYOUT IS NOT EMPTY (contains listview). IF EMPTY - IT WORKS FINE.
listener.onPageSelected(0);
}

}
}

应用程序在线抛出异常

this.notifyDataSetChanged();

如果布局不为空。

我找不到如何避免此异常并正确替换 fragment 的解决方案。

已编辑

https://android.googlesource.com/platform/frameworks/support/+/jb-mr1-dev/v4/java/android/support/v4/app/Fragment.java

我检查过 Fragment 类包含以下方法:

public void setArguments(Bundle args) {
if (mIndex >= 0) {
throw new IllegalStateException("Fragment already active");
}
mArguments = args;
}

也许我添加 fragment 的方式不正确..

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