ViewPager can be a bit finicky at times.
The way to deal with the second issue is simpler: are you implementing getCount()? That should solve your second problem- set the length as the total number of cards you have (minus one, of course). That should make it where it won't go past the total number of cards- if you leave it blank, it will crash when you go to the last card (as it tries to load the next but finds nothing).
The first issue is a bit more complicated: how do you make it run code when it gets to the last location? The best way to do this would be to implement an onPageChangeListener wherever your ViewPager is implemented (likely onCreate()). Here's an example:
mPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int pos) {
if (pos == lastPage) {
//display your XML or do anything else you want to do when done here
}
}
};
yourViewPager.setOnPageChangeListener(mPageChangeListener);
Hopefully this solves your other problem!