I recently have started programming again for the android and am currently working on a app that I would like to implement horizontal view pagers so the user in one instance can swipe between two pages to enter/update information and in a later instance swipe between four pages to view information that they have entered/updated. I am basically making an electronic character sheet for roleplaying games.
I have been working off of these tutorials for horizontal view pagers:
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
http://manishkpr.webheavens.com/android-viewpager-example/
My question is of all the tutorials I have seen, the h. view pager is used off of the main activity screen, is there a way to implement the horizontal view pager off of a subsequent screen? Every time I have tried to implement the code to work off of a page other than a main screen it has crashed as soon as I got to that page.
So, long story short, has anyone successfully implemented horizontal view pagers on a non main page and if so, how?
I hope that I have made sense, but if you have any further questions please let me know!
08-24 01:44:34.310: I/ActivityManager(144): START {cmp=com.echaractersheet/.CharacterStats1} from pid 15115
08-24 01:44:34.360: D/AndroidRuntime(15115): Shutting down VM
08-24 01:44:34.360: W/dalvikvm(15115): threadid=1: thread exiting with uncaught exception (group=0x40a581f8)
08-24 01:44:34.370: E/AndroidRuntime(15115): FATAL EXCEPTION: main
08-24 01:44:34.370: E/AndroidRuntime(15115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.echaractersheet/com.echaractersheet.CharacterStats1}: java.lang.NullPointerException
characterstats.xml:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/characterstatspager" />
characterstats1.xml and characterstats2.xml are the two pages I want to swipe between
CharacterStatsPagerAdapter.java:
…
public class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId =0;
switch (position) {
case 0:
resId = R.layout.characterstats1;
break;
case 1:
resId = R.layout.characterstats2;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
CharacterStats1.java:
CharacterStatsPagerAdapter adapter = new CharacterStatsPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.characterstatspager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
well the pager will work with the data in the adapter, so as long as you keep the objects in the adapter, you are good to go. so maybe before going to the next screen you should save the objects in the adapter (maybe to a static field in another class, just for a moment) and when you get to the new page you can retrieve the objects and put them in the adapter of the new viewpager and add the new pages. That is what i will do in your case.
UPDATE:
this is just an example, this needs more lines of code, but is a good start.
The View Pager Adapter
Main Class
}
Second Class
As i said, this is JUST AN EXAMPLE, the Fields Class should be changed for your Objects, the ViewPagerAdapter is where you keep the list of pages that you will pass to the next activity.
And the static field on the secondactivity is just to be use as a bridge between activities and
should not be overuse, thats is why after fetching the data you need to set to NULL to know that you
already grabbed the value. Hope it helps.
NOTE: THIS CODE WILL NOT WORK AS IT IS, need more code.