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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:26:53+00:00 2026-05-29T23:26:53+00:00

I’m able to build an activity with a viewpager from the various examples on

  • 0

I’m able to build an activity with a viewpager from the various examples on the web, but can I no longer use the views as they were in the original activity? Take for example the following:

public class SimpleViewPagerActivity extends Activity {

    LinearLayout thisIsWhatBreaks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            MyPagerAdapter adapter = new MyPagerAdapter();
            ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(2);

            thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);

    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            thisIsWhatBreaks.setBackgroundColor(Color.BLACK);

        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            thisIsWhatBreaks.setBackgroundColor(Color.RED);
        }   
    }


    public void farLeftButtonClick(View v)
    {
            Toast.makeText(this, "Far Left Button Clicked", Toast.LENGTH_SHORT).show(); 

    }

    public void farRightButtonClick(View v)
    {
            Toast.makeText(this, "Far Right Elephant Button Clicked", Toast.LENGTH_SHORT).show(); 

    }

    private class MyPagerAdapter extends PagerAdapter {

            public int getCount() {
                    return 5;
            }

            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.farleft;
                            break;
                    case 1:
                            resId = R.layout.left;
                            break;
                    case 2:
                            resId = R.layout.middle;
                            break;
                    case 3:
                            resId = R.layout.right;
                            break;
                    case 4:
                            resId = R.layout.farright;
                            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 void finishUpdate(View arg0) {
                    // TODO Auto-generated method stub

            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                    return arg0 == ((View) arg1);

            }

            @Override
            public void restoreState(Parcelable arg0, ClassLoader arg1) {
                    // TODO Auto-generated method stub

            }

            @Override
            public Parcelable saveState() {
                    // TODO Auto-generated method stub
                    return null;
            }

            @Override
            public void startUpdate(View arg0) {
                    // TODO Auto-generated method stub

            }

    }

}

I originally had the thisIsWhatBreaks LinearLayout in main.xml, but have now moved it to farleft.xml, which is one of the layouts inflated in the PagerAdapter.

Therefore, I can no longer use
LinearLayout thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
to assign value to the LinearLayout. Where and how do I do this?

How could I once again assign value to the LinearLayout so that it can be modified in the OnConfurationChanged() method as shown within this activity class? I keep getting nullpointerexceptions all over my activity now that I’ve moved most of the views from main.xml to farleft.xml and can’t seem to find a way to make this work.

  • 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-05-29T23:26:54+00:00Added an answer on May 29, 2026 at 11:26 pm

    All I needed to do was move :

    thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);

    to the case which inflates the current layout.xml inside of instantiateItem() of the PagerAdapter.

    My problem with the nullpointerexceptions was that all of my views were being acted upon in onCreate(). I had to move all of the views from onCreate() to the case statement inside of instantiateItem();

    See the corrected code below….just in case anyone else runs into this problem….

    public class SimpleViewPagerActivity extends Activity {
    
    LinearLayout thisIsWhatBreaks;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            MyPagerAdapter adapter = new MyPagerAdapter();
            ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(2);
    
            //REMOVE THE LINEAR LAYOUT FROM BEING INSTANTIATED HERE!
    
    }
    
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    
            thisIsWhatBreaks.setBackgroundColor(Color.BLACK);
    
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            thisIsWhatBreaks.setBackgroundColor(Color.RED);
        }   
    }
    
    
    private class MyPagerAdapter extends PagerAdapter {
    
            public int getCount() {
                    return 5;
            }
    
            public Object instantiateItem(View collection, int position) {
    
              view = new View(MainHome.this);
    
              final LayoutInflater inflater = (LayoutInflater) MainHome.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
              switch(position){
    
              case 0 : 
                        view = inflater.inflate(R.layout.farleft, null);
                        thisIsWhatBreaks= (LinearLayout)v.findViewById(R.id.layoutThatUsedToAlwaysBeNull);
    
                         ***Now you can modify thisIsWhatBreaks***
                        thisIsWhatBreaks.setBackgroundColor(Color.black);
    
                  break;
    
              case 1: 
    
                        view = inflater.inflate(R.layout.farright, null);
    
    
                  break;
    
              default :
    
                  break;    
              }
    
             ((ViewPager) collection).addView(v,0);
             return v;  
    
            }
    
            @Override
            public void destroyItem(View arg0, int arg1, Object arg2) {
                    ((ViewPager) arg0).removeView((View) arg2);
    
            }
    
            @Override
            public void finishUpdate(View arg0) {
                    // TODO Auto-generated method stub
    
            }
    
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                    return arg0 == ((View) arg1);
    
            }
    
            @Override
            public void restoreState(Parcelable arg0, ClassLoader arg1) {
                    // TODO Auto-generated method stub
    
            }
    
            @Override
            public Parcelable saveState() {
                    // TODO Auto-generated method stub
                    return null;
            }
    
            @Override
            public void startUpdate(View arg0) {
                    // TODO Auto-generated method stub
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.