user634545
user634545

Reputation: 9419

ViewPager fragments, not attached?

I have a problem with ViewPager. It won't display any of my pre-definied fragments, it just displays a black screen. The onCreateView method is called from inside the fragments.

public class TestViewPager extends FragmentActivity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.test_view_pager);
    }
}

public class TestFragment extends Fragment {

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

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

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment 
        class="com.test.TestFragment"
        android:id="@+id/embedded1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <fragment 
        class="com.test.AnotherTestfragment"
        android:id="@+id/embedded2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>   
</android.support.v4.view.ViewPager>

Upvotes: 1

Views: 1387

Answers (1)

SimonVT
SimonVT

Reputation: 1080

With ViewPager you must implement a PagerAdapter and supply the fragments from there. You can not define views/fragments to be shown in the ViewPager from XML, as it will simply not draw them.

Edit: Since you want to show Fragments, you must use FragmentPagerAdapter.

Upvotes: 3

Related Questions