1

このフラグメントをインスタンス化すると、stackoverflow エラーが発生します。メイン アクティビティ クラスで定義されたナビゲーション ドロワーを使用して、このフラグメントを選択しようとしています。

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;
        }
    }
}

XML ファイル 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 はあまり有益ではありませんでした:

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 に、他の 2 つのフラグメントが取り込まれた 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他の を含めることはあなたの意図Fragmentですか? そうでない場合、この設計は正しくありません。はViewPagerで使用されるレイアウトに属しますActivityFragment他の を内部に表示したい場合は、こちらMyFragmentを参照してください。

元の回答: あなたの問題は、自体MyFragmentPagerAdapterの で新しい を作成していることです。ページャー アダプターは、の内部クラスであってはなりません。ページャー アダプターは、 のビュー ページャーと連携して、いつでも表示する必要があるの種類を決定します。onViewCreatedFragmentFragmentActivityFragment

編集:他の回答に関するコメントを見たからといってgetItem、の新しいインスタンスを返す必要がありFragmentます。それは根本的な問題ではありませんでした。

于 2013-10-07T08:27:18.163 に答える