Reputation: 22988
I am following the Android Programming: The Big Nerd Ranch Guide and there is an example using ViewPager through the 2 files:
The file res/values/ids.xml defines an id in XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="viewPager" />
</resources>
And the Java source file CrimePagerActivity.java uses that id:
public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
I have tried to change the example to have a ViewPager
object being inflated from an XML layout file res/layout/crime_pager.xml (instead of just using an id):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager" />
</LinearLayout>
And then I am trying to use that object in the CrimePagerActivity.java:
public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crime_pager);
mViewPager = (ViewPager)findViewById(R.id.view_pager);
Unfortunately, this does not work and fails with a RuntimeException
and little info (here fullscreen):
What is the problem here please?
Upvotes: 1
Views: 2219
Reputation: 18977
change:
<ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager" />
to
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Upvotes: 2