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 8880721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:12:48+00:00 2026-06-14T20:12:48+00:00

I have a ViewPager which contains 3 fragments which is working fine. I start

  • 0

I have a ViewPager which contains 3 fragments which is working fine. I start an activity from my any of my fragments inside the viewpager and the activity is displayed. After that when i press the back button, my application crashes with the following excepion:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {AppName.Activities/AppName.Activities.ViewPagerActivity}: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:957)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
    at java.util.ArrayList.set(ArrayList.java:484)
    at android.support.v4.app.FragmentStatePagerAdapter.destroyItem(FragmentStatePagerAdapter.java:97)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:415)
    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:271)
    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:244)
    at AppName.Activities.ViewPagerActivity.setUpView(ViewPagerActivity.java:36)
    at AppName.Activities.ViewPagerActivity.onStart(ViewPagerActivity.java:28)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
    at android.app.Activity.performStart(Activity.java:3791)
    at android.app.Activity.performRestart(Activity.java:3821)
    at android.app.Activity.performResume(Activity.java:3826)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)

The following is my code for ViewPagerActivity that extends FragmentActivity::

public class ViewPagerActivity extends FragmentActivity
{
    private ViewPager mViewPager;
    private ViewPagerAdapter adapter;
    boolean flag = false;

    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        setContentView(R.layout.view_pager);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        setUpView();
    }

    private void setUpView()
    {
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.settings:
                startActivity(new Intent(this, AppSettingsActivity.class));
                return true;
            case R.id.addSituationMenu:
                Intent i = new Intent(this, MainLayout.class);
                i.putExtra("parentActivity", "SplashScreenLayout");
                startActivity(i);
                return true;
            case R.id.historyActivity:
                startActivity(new Intent(this, HistoryActivity.class));
                return true;
            case R.id.chartActivity:
                startActivity(new Intent(this, ViewPagerActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

The line mViewPager.setCurrentItem(0); causes the crash.

This is the ViewPagerAdapter code:

public class ViewPagerAdapter extends FragmentStatePagerAdapter
{
    private final Context context;
    ArrayList<ArrayList<Object>> data;
    int totalMoodEntries = 0;
    static Fragment f = null;

    public ViewPagerAdapter(Context mcontext, FragmentManager fm)
    {
        super(fm);
        context = mcontext;
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
            {
                f = new ChartFragment(context, totalMoodEntries, data);
                break;
            }
            case 1:
            {
                f = new ViewRecordsFragment(context, data);
                break;
            }
            case 2:
            {
                f = new LearnMoreFragment(context);
                break;
            }
        }
        return f;
    }

    @Override
    public int getCount()
    {
        return 3;
    }
}
  • 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-14T20:12:49+00:00Added an answer on June 14, 2026 at 8:12 pm

    Can you try by moving following line in the onCreate() method:

    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
    mViewPager.setAdapter(adapter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using fragments to show images/pages.I have one Activity(Main) which contains all the
I have a ViewPager within a Fragment which contains three Fragments we'll call V1,
I have a Fragment activity that contains 6 fragments. Every fragment is instance of
I'm fairly new to Fragments and I have an activity which contains a few
I have a ViewPager with 3 fragments into it. Everything works fine with two
I have implemented HorizontalListView inside ViewPager . ViewPager works fine but a child of
So I have an Activity which has a FragmentPagerAdapter and inside each fragment I
I have a project using a ViewPager which works correctly: I can page from
I created a ViewPager and everything is working fine, however I want to have
I have a ViewPager (extends FragmentPagerAdapter ) which holds two Fragments . What I

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.