IllegalStateException:无法更改片段ViewPager的标记

时间:2016-02-22 18:33:19

标签: android android-fragments android-viewpager

我的ViewPager会在点击按钮时删除片段。但是当我点击删除时,我会收到“canot change tag”错误。浏览其他类似的问题,它是getItem的问题,或PagerAdapter中的getCount,我认为我的实现很好。请指教。

这是要删除的片段

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    displayActivity = (DisplayActivity) getActivity();
    initView();
}
 Button delete = (Button) getActivity().findViewById(R.id.dont_show_button_1);
    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayActivity.removeCurrentFragment(count-1);
        }
    });

我的PagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

long baseId = 0;
public static List<Fragment> fragmentArrayList = new ArrayList<Fragment>();

public PagerAdapter(FragmentManager fm){
    super(fm);
    fragmentArrayList.add(new FragmentOne());
    fragmentArrayList.add(new FragmentTwo());
    fragmentArrayList.add(new FragmentThree());
}
@Override
public Fragment getItem(int position) {
    return fragmentArrayList.get(position);
}

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

@Override
public int getItemPosition(Object object){
    return PagerAdapter.POSITION_NONE;
}

@Override
public long getItemId(int position) {
    // give an ID different from position when position has been changed
    return baseId + position;
}

/**
 * Notify that the position of a fragment has been changed.
 * Create a new ID for each position to force recreation of the fragment
 * @param n number of items which have been changed
 */
public void notifyChangeInPosition(int n) {
    // shift the ID returned by getItemId outside the range of all previous fragments
    baseId += getCount() + n;
}
}

这是托管ViewPager的活动

public class DisplayActivity extends FragmentActivity {

ViewPager viewPager;
PagerAdapter pagerAdapter;
String TAG = "DisplayActivity";

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

    viewPager = (ViewPager) findViewById(R.id.pager);

    pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
}

public void removeCurrentFragment(int position){
    pagerAdapter.fragmentArrayList.remove(position);
    pagerAdapter.notifyChangeInPosition(1);
    pagerAdapter.notifyDataSetChanged();
    Log.e(TAG, "in removeCurrentFragment");
}

我收到了错误:

IllegalStateException: Can't change tag of fragment FragmentTwo{170c98f1 #1 id=0x7f0c006d android:switcher:2131492973:1}: was android:switcher:2131492973:1 now android:switcher:2131492973:3

2 个答案:

答案 0 :(得分:1)

问题在于

public class PagerAdapter extends FragmentPagerAdapter

将其更改为

public class PagerAdapter extends FragmentStatePagerAdapter

启用正确删除。

这可归因于“FragmentStatePagerAdapter处理不在当前和可遍历视图之外的视图。” 正如这篇文章的建议 Remove Fragment Page from ViewPager in Android

答案 1 :(得分:0)

通过查看您的代码,我认为您的计数值可能是错误的。

Button delete = (Button) getActivity().findViewById(R.id.dont_show_button_1);
delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       // here
        displayActivity.removeCurrentFragment(count-1);
        //change to
      displayActivity.removeCurrentFragment(adapter.getCount()-1);
    }
}); 

希望它有所帮助。请将你的整个代码发布到你获得count变量值的地方。如果解决方案无法解决您的问题,请发布整个代码。 例如,请参阅此处: - https://androidruler.wordpress.com/2016/02/22/working-with-viewpager-as-a-images-slider/