Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:27:19+00:00 2026-06-10T04:27:19+00:00

I recently have started programming again for the android and am currently working on

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:27:20+00:00Added an answer on June 10, 2026 at 4:27 am

    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.

    public class Fields {
        private String name;
        private String lastName;
        private int age;
        private boolean male;
    
        public Fields(){
            this.name = "";
    
            this.lastName = "";
        }
    
        public Fields(String name, String lastName, int age, boolean male) {
            this.name = name;
            this.lastName = lastName;
            this.age = age;
            this.male = male;
        }
    
        public View getRepresentation(Context mContext){
            /*Create the view that ViewPager will display*/
            LinearLayout layout = new LinearLayout(mContext);
            layout.addView(new TextView(mContext)); //Name
            layout.addView(new TextView(mContext)); //LastName
            layout.addView(new TextView(mContext)); //Age
            layout.addView(new CheckBox(mContext)); //Male/Female
            return layout;
        }
    
    }
    

    The View Pager Adapter

    public class ViewPagerAdapter extends PagerAdapter {
    
        private List<Fields> pages;
        private Context mContext;
    
        public ViewPagerAdapter( Context mContext )
        {
            this.mContext = mContext;
        }
    
        public void setPages(List<Fields> pages){
            this.pages = pages;
        }
    
        public List<Fields> getPages(){
            return this.pages;
        }
    
        @Override
        public int getItemPosition(Object object) {
               return POSITION_NONE;
        }
    
        @Override
        public int getCount()
        {
            return pages.size();
        }
    
        @Override
        public Object instantiateItem( View pager, int position )
        {
            View view = pages.get(position).getRepresentation(mContext);
            view.setId(position);
            return view;
        }
    
        @Override
        public void destroyItem( View pager, int position, Object view )
        {
            ((ViewPager)pager).removeViewInLayout( (View) view );
        }
    
        @Override
        public boolean isViewFromObject( View view, Object object )
        {
            return view.equals( object );
        }
    
    }
    

    Main Class

    public class MainActivity extends Activity {
    
    private Context mContext;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getApplicationContext();
        setContentView(R.layout.viewpager_layout);
        final List<Fields> fields = new ArrayList<Fields>();
        fields.add(new Fields());
        ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
        Button nextActivity = (Button) this.findViewById(R.id.nextAct);
        ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
        mAdapter.setPages(fields);
        mPager.setAdapter(mAdapter);
        nextActivity.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                SecondActivity.pages = fields;
                Intent intent = new Intent(mContext, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    

    }

    Second Class

    public class SecondActivity extends Activity {
    
        public static List<Fields> pages;
        private Context mContext;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.mContext = getApplicationContext();
            setContentView(R.layout.viewpager_layout);
    
            final List<Fields> fields = pages;
            pages = null;
            fields.add(new Fields()); //Add the new Pages
            ViewPager mPager = (ViewPager) this.findViewById(R.id.viewpager);
            Button nextActivity = (Button) this.findViewById(R.id.nextAct);
            ViewPagerAdapter mAdapter = new ViewPagerAdapter(mContext);
            mAdapter.setPages(fields);
            mPager.setAdapter(mAdapter);
            nextActivity.setVisibility(View.GONE);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just recently started out programming for Android, and have decided that I
I recently started programming my first Cocoa app. I have ran into a problem
Recently I have started working on a program that will monitor the packets of
I have recently started programming android apps (put simply, i have next to no
I have only recently started web programming and I am pretty much amazed that
I have recently started programming in WPF and bumped into the following problem. I
I have recently started programming in PHP. I am building a cart in PHP.
I recently started programming C on Mac OS X (10.7.4). I have the various
Recently I have started learning rails and was a little surprised that the default
I have recently started seeing user agents like Java/1.6.0_14 (and variations) on my site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.