FragmentActivity中的Android PagerAdapter IllegalStateException

时间:2017-07-09 10:49:04

标签: android android-viewpager fragment illegalstateexception

我收到如下错误,我无法弄清楚它为什么会发生。我没有关于错误的更多信息。

我有一个包含50个项目的Android ListView。当您单击某个项目,然后打开第二个屏幕(带分页器的片段活动)时,此Fragment活动将显示ListView中所有项目,从ListView中的单击项目开始,直到最后一个项目具有向左或向右滑动的可能性。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!--
Some text views for information
-->
    <RelativeLayout
        android:id="@+id/container_veiw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Below view holds view 2 at the bottom -->
        <RelativeLayout
            android:id="@+id/animate_parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:fillAfter="true"
            android:fillEnabled="true">

            <!-- Below relative layout is xepected to be hidden behind top area -->
            <!-- Below is view 1 in diagram -->
            <RelativeLayout
                android:id="@+id/animate_child"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/black_background">

                <LinearLayout
                    android:id="@+id/some_other_views"
                    android:layout_width="match_parent"
                    android:layout_height="70dp"
                    android:layout_alignParentTop="true"
                    android:layout_margin="16dp"
                    android:animateLayoutChanges="true"
                    android:orientation="horizontal">

                    <!-- Some child views -->

                </LinearLayout>

            </RelativeLayout>

            <!-- This is view 2 -->
            <RelativeLayout
                android:id="@+id/view2"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_below="@id/animate_child"
                android:background="@android:color/white">

                <!-- Some child views -->

            </RelativeLayout>

        </RelativeLayout>
        <!-- At this place after animation empty area is getting created -->

        <!-- Below relative layout is expected to be just below @id/animate_parent -->
        <!-- This is view 3 -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/animate_parent"
            android:background="#ffffff">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:background="#ffffff"
                android:fadeScrollbars="false"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                tools:listitem="@layout/fragment_document" />
        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

public class ScreenSlidePagerActivity extends FragmentActivity {
    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    public static int NUM_PAGES = 50;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

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

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);

    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() <= 0) {

           //start an intent
        } else {
            // Otherwise, select the previous step.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Bundle bundle = new Bundle();
            bundle.putSerializable("content", "bla bla content" );

            MyFragment fragmentObj= new MyFragment();
            fragmentObj.setArguments(bundle);
            return fragmentObj;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }

    }


}