10
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.

Henrik
  • 490
  • 2
  • 10
  • 16

3 Answers3

24

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.

dennisdrew
  • 4,399
  • 1
  • 25
  • 25
1

You can avoid all this dirty job just adding setContentView(R.layout.youlayout) before findViewById

Moesio
  • 3,100
  • 1
  • 27
  • 36
0

Just guess as you don't show xml for inflated. Do you use +@id. If not give it a try.

Warpzit
  • 27,966
  • 19
  • 103
  • 155