25

嗨,我在使用 pageViwer 和 Adapter 滑过 3 个片段时遇到此错误。

这是我的页面适配器

public class FreedomPageAdapter extends FragmentPagerAdapter {

    private List<Fragment> listFragment;


    public FreedomPageAdapter(FragmentManager fm, List<Fragment> listFragment) {

        super(fm);
        this.listFragment = listFragment;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
     */
    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        return listFragment.get(position);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.view.PagerAdapter#getCount()
     */
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listFragment.size();
    }

这是我定义综合浏览量的方式

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

        viewPager.setOnPageChangeListener(this);

        savedListFragment = new SubscribedFragment();

        fragmentList.add(savedListFragment);

        fragmentList.add(savedListFragment);

        fragmentList.add(savedListFragment);

        viewPager.setAdapter(new FreedomPageAdapter(fragmentManager(),
                fragmentList));



    private FragmentManager fragmentManager() {
        return getSupportFragmentManager();
    }

    private FragmentTransaction getFragmentTransaction() {
        return fragmentManager().beginTransaction();
    }

页面查看监听器

/*
     * (non-Javadoc)
     * 
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#
     * onPageScrollStateChanged(int)
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled
     * (int, float, int)
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
     * (int)
     */
    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);

    }

自定义片段对象

   public class SubscribedFragment extends Fragment{

    /* (non-Javadoc)
     * @see android.app.Fragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

    /* (non-Javadoc)
     * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.discussion_list_layout, container);
    }

}

错误的完整堆栈跟踪:

    05-02 19:15:44.599: E/AndroidRuntime(19839): FATAL EXCEPTION: main
05-02 19:15:44.599: E/AndroidRuntime(19839): java.lang.IllegalStateException: Can't change tag of fragment SubscribedFragment{41157420 id=0x7f070005 android:switcher:2131165189:0}: was android:switcher:2131165189:0 now android:switcher:2131165189:1
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer.doFrame(Choreographer.java:532)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Handler.handleCallback(Handler.java:725)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Looper.loop(Looper.java:137)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.app.ActivityThread.main(ActivityThread.java:5226)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at java.lang.reflect.Method.invoke(Method.java:511)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-02 19:15:44.599: E/AndroidRuntime(19839):    at dalvik.system.NativeStart.main(Native Method)
4

6 回答 6

90

这可能是因为您将相同的片段实例添加到列表中三次。您应该为每个页面创建一个新实例。

另外,我建议您调查FragmentStatePagerAdapter一下您是否对开发不太感兴趣。如果您想从主要活动中刷新片段的内容,这可能是一个更好的选择。

于 2013-05-02T18:38:00.687 回答
8

以下方法应为每个选项卡返回一个片段:

@Override
public Fragment getItem(int position) {
    //Add some code
    //to verify that it will not return null
    return listFragment.get(position);
}

代码没问题,但要确保 listFragment 包含三个不同的 Fragment,并且该 Fragment 不为空。

于 2014-03-03T19:11:42.690 回答
2

很可能,您发送的片段实例为空。永远不要发送片段实例为空。

    public static MyFragment getInstance() {
    MyFragment myFragment = new MyFragment();
    return myFragment;
}

并且在寻呼机适配器中始终使用@Override public Fragment getItem(int position) { return fragments[position]; }

或者

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

根据您的片段列表。也请首选链接FragmentPagerAdapter

注意:-永远不要将上下文内存用作静态内存

于 2020-01-21T06:45:23.027 回答
1

就我而言,这是由于返回了一些 null 片段,而 getItem(position) 函数中没有处理该片段。希望它可以帮助某人。

于 2019-03-11T08:38:07.123 回答
0

您已经创建了一个 SubscribedFragment() 类的实例

savedListFragment = new SubscribedFragment();

然后将此对象的列表传递给 ViewPager Adpater:

FreedomPageAdapter(FragmentManager fm, List<Fragment> listFragment)

它在其构造函数中接收片段类型列表。由于构造函数接收到 Fragment 类型的 List,因此在将其添加到列表之前尝试将每个 savedListFragment 转换为 Fragment,然后将其传递给 adpater 的构造函数:

fragmentList.add((Fragment)savedListFragment);
viewPager.setAdapter(new FreedomPageAdapter(fragmentManager(), fragmentList));
于 2013-05-02T18:46:10.843 回答
0

就我而言,我没有返回特定位置的片段实例。我认为 FragmentPagerAdapter 正在接收 null 来代替 TAG。这就是为什么它显示“无法更改片段标签”如果我错了请纠正我。

于 2018-09-07T14:32:45.280 回答