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

  • Home
  • SEARCH
  • 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 6791595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:51:56+00:00 2026-05-26T17:51:56+00:00

public class HSPTabletTestActivity extends Activity { private class MyPagerAdapter extends PagerAdapter { public int

  • 0
public class HSPTabletTestActivity extends Activity {
private class MyPagerAdapter 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.lighttab;
            break;
        case 1:
            resId = R.layout.securitytab;
            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 have this code up there ^^ … Pretty much taken directly from http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ … I’m still pretty new in the world of android developement :/

Now I’m trying to access a spinner control and some buttons inside the “pages”. But findViewById keeps returning null!

I remember something about the layout doesn’t exist really inside the code and has to be inflated first, which is happening in the instantiateItem() function. And it’s pretty obvious that it’s going into the “view” variable. But when I call
view.findViewById(R.id.light1_off);

which is a button by the way, it is always returning ZERO!
I even made sure it only called when it was actually that page being loaded. But no matter what I did, it always keep returning a null and I get a nasty nullpointer exception.

Could someone help me out here? I’m pretty much out of ideas, and Google isn’t of any help, already been to page 5 on about 10 different search terms.

  • 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-26T17:51:56+00:00Added an answer on May 26, 2026 at 5:51 pm

    After much frustration and pulling my hair out over this issue, I have solved it! At least for me. Assuming you used the tutsplus tutorial like I did, you have separate XML files for your screens. Now, I assume those layout XMLs contain layouts within them (ie. LinearLayout, RelativeLayout, etc.). Now, those layouts contain your button and other widgets. What you have to do to be able to findViewById is, in the actual switch statement in the instatiateItem method, initialize your button in this way:

    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.lighttab;
            View view = inflater.inflate(resId, null);
            RelativeLayout layout=(RelativeLayout)view.findViewById(R.id.relLayout);
            Button button=(Button)layout.findViewById(R.id.button);
            ((ViewPager) collection).addView(view, 0);
            return view;
        case 1:
            resId = R.layout.securitytab;
            ...
            ...
            return view;
        }
    }
    

    So…first you have to inflate the view, then initialize the layout held within the view by casting the type of layout (RelativeLayout) and calling .findViewById(resource id). Then, you initialize the actual widget you’re looking for by doing the same, but casting the widget type (Button) and calling .findViewById(resource id).

    This worked for me, so I hope this saves you some trouble! Took forever for me to figure out.

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

Sidebar

Related Questions

public class WordDisplay extends Activity { private int level; private int group; private int
public class upload extends Activity { private static final int CAMERA_REQUEST = 1888; private
public class PackageTabActivity extends ListActivity{ HashMap<String,Object> hm ; ArrayList<HashMap<String,Object>> applistwithicon ; private static final
Public Class FooBar { private int _foo; public int Foo { get { return
public class BobDatabase extends SQLiteOpenHelper{ private static final String DATABASE_NAME = bob.db; private static
public class Bird { private static int id = 0; private String kind; public
public class Browser1Activity extends Activity { TextView url; WebView ourBrow; @Override protected void onCreate(Bundle
public class Offer_Popup extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.offer_popup); //newly
public class SuperUser extends User implements Serializable{ private static final long serialVersionUID = 1L;
public class MyBindingList : BindingList<int> { public MyBindingList() { } private BindingList<int> temp; public

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.