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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:51:02+00:00 2026-06-11T04:51:02+00:00

I’m new to Android developing and I would really appreciate some help here. I’m

  • 0

I’m new to Android developing and I would really appreciate some help here.

I’m using a fragment that contains a TextView and I’m using 5 instances of the same MyFragment class.

In the activity, i got a button and a ViewPager, and I need the button to update all the fragment instances content, whenever its clicked.

Here’s the Activity

public class MainActivity extends FragmentActivity {

final static String[] CONTENT = {"a", "b"};
ViewPager pager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<MyFragment> fragments = new Vector<MyFragment>();
    for(int i = 0; i < 5; i++){
        MyFragment fragment = new MyFragment(CONTENT);
        fragments.add(fragment);
    }
    PagerAdapter adapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
    pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(adapter);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //method that isn't working
            PagerAdapter adapter = (PagerAdapter)pager.getAdapter();
            for(int i = 0; i < 5; i++){
                MyFragment fragment = (MyFragment) adapter.getItem(i);
                fragment.textView.setText(fragment.content[1]);
            }
        }
    });
}
}

The Fragment

public class MyFragment extends Fragment{

String[] content;
    TextView textView;

public MyFragment(String[] content) {
    this.content = content;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_content, container, false);
    textView = (TextView) view.findViewById(R.id.textView1);
    textView.setText(content[0]);
    return view;
}

}

And the FragmentPagerAdapter

public class PagerAdapter extends FragmentPagerAdapter{

List<MyFragment> fragments;

public PagerAdapter(FragmentManager fm, List<MyFragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int arg0) {
    return fragments.get(arg0);
}

@Override
public int getCount() {
    return fragments.size();
}

}

The OnClick method gives me a NullPointerException whenever i try to access a fragment from the adapter which is less than adapter.getCurrentItem() – 1, or more than adapter.getCurrentItem() + 1.

Any idea on how to update all the fragments at the same time?

Thanks in advance.

  • 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-11T04:51:04+00:00Added an answer on June 11, 2026 at 4:51 am

    The easiest way to update those fragments is to use your code and set the number of fragments that the ViewPager holds in memory to the number of total fragments – 1(so all fragments are valid no matter at what page you are). In your case:

    pager.setOffscreenPageLimit(4); // you have 5 elements
    

    You can still use the method from my comment with the method onPageScrollStateChanged(so the update will start the moment the user starts swiping) to see when the user is starting to swipe the pager and update the fragments to the left and right of the currently visible fragment, but this will be a bit difficult to get right so I recommend to go with the first option.

    Some points regarding your code containing fragments:

    If you nest the fragment class make it static so you don’t tie it to the activity object.
    Don’t create a constructor for a Fragment besides the default one. If the framework needs to recreate the fragment it will call the default constructor and if it is not available it will throw an exception. For example, try to change the orientation of the phone/emulator and see what happens(this is one of the cases when Android will recreate the fragments). Last, use a custom name for the ViewPager‘s adapter, you use PagerAdapter which is the name of the super class of FragmentViewPager and it’s very confusing for someone reading your code.

    If you need to pass data to the Fragment you could use a creation method like the one below:

    public static MyFragment newInstance(String text) {
            MyFragment f = new MyFragment();
            Bundle b = new Bundle();
            b.putString("content", text);
            f.setArguments(b);
            return f;
        }
    

    The text will be available in MyFragment by using getArguments().getString("content");

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.