1

When I instantiate this fragment I get a stackoverflow error. I'm trying to have this fragment selected using the navigation drawer defined in my main activity class.

public class MyFragment extends Fragment {

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.page_meet, container, false);
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager);
        MyFragmentPagerAdapter mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return newInstance();
        }

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

Here is the XML file page_meet.xml:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#00FF00" >


</LinearLayout>

</android.support.v4.view.ViewPager>

Logcat wasn't very informative:

10-07 03:19:27.910: E/AndroidRuntime(27875): FATAL EXCEPTION: main
10-07 03:19:27.910: E/AndroidRuntime(27875): java.lang.StackOverflowError
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11890)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirection(View.java:11892)
10-07 03:19:27.910: E/AndroidRuntime(27875):    at android.view.View.canResolveLayoutDirec
4

2 回答 2

0

谢谢您的回答。我确实希望 MyFragment 包含一个 ViewPager,其中填充了另外两个片段。堆栈溢出错误是因为我在没有意识到的情况下递归调用了 MyFragment.newInstance()。我尝试用它自己的片段类填充视图寻呼机。我通过在其中嵌套另一个片段类并在 getItem() 中调用其 newInstance() 方法来解决此问题。

完成的工作代码:

public class MyFragment extends Fragment {

    public static final MyFragment newInstance() {
        return new MyFragment();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.page_meet, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager);
        MyFragmentPagerAdapter mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return MeetFragment.newInstance(position);
        }

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

    public static class MeetFragment extends Fragment {
        public static final String ARG_POSITION = "position";

        public static final MeetFragment newInstance(int position) {
            MeetFragment fragment = new MeetFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_POSITION, position);
            fragment.setArguments(args);
            return fragment;
        }

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

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView;
            int position = getArguments().getInt(ARG_POSITION);
            switch(position) {
            case 0:
                rootView = inflater.inflate(R.layout.page_meet_0, container, false);
                break;
            case 1:
                rootView = inflater.inflate(R.layout.page_meet_1, container, false);
                break;
            default:
                rootView = null;
                break;
            }
            return rootView;
        }
    }
}
于 2013-10-07T21:05:25.147 回答
0

我写了我的答案,却没有意识到你ViewPager在 XML 中有一个Fragment. 您是否打算MyFragment包含其他Fragments?如果不是,那么这个设计是不正确的。ViewPager属于 中使用的布局Activity。如果您确实想在Fragment里面显示其他 s MyFragment,请参见此处

原始答案:您的问题是您正在创建一个新MyFragmentPagerAdapter的本身onViewCreatedFragment寻呼机适配器不应该是Fragment. 寻呼机适配器与视图寻呼机一起工作,以确定在任何给定时间应显示Activity哪种类型。Fragment

编辑:仅仅因为我看到你对另一个答案的评论,getItem应该返回一个新的Fragment. 那不是根本问题。

于 2013-10-07T08:27:18.163 回答